0

I have below javascript object and wanted to some property based on conditionally. If the type is not match then don't include those property in object. Don't wanted to set null or undefined conditionally to start and end property. Just wanted to remove.

let startDate = "02/03/2021";
let endDate = "03/03/2021";

const update = (type, value) => {
  //wanted to exclude start and end property from below object other than type "today". 
  //Don't wanted to set null or undefined conditionally to start and end property. Just wanted to remove
   updateDate({
     start: startDate,
     end: endDate,
     type: type === "today" ? "TD" : type,
     value: value === "today" ? "TD" : value 
   })
}
4
  • Other Question doesn't resolve my problem. Please open this question Commented Apr 7, 2021 at 10:32
  • Have you seen this answer specifically Commented Apr 7, 2021 at 10:37
  • Try this let startDate = "02/03/2021"; let endDate = "03/03/2021"; const updateObj = (type, val) => { return {...(type === "today" ? {start: startDate}: {}),...(val === "today" ? {end: endDate}: {})}}; console.log(updateObj("today"));console.log(updateObj("test", "today")); Commented Apr 7, 2021 at 10:39
  • OR you can delete object props based on condition. let object = { start: startDate, end: endDate, type: type === "today" ? "TD" : type, value: value === "today" ? "TD" : value } if (type !== "today"){ delete object.start; //you can delete props delete object.end; } updateDate(object) Commented Apr 7, 2021 at 10:43

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.