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 
   })
}
Nithish
  • 5,393
  • 2
  • 9
  • 24
James
  • 371
  • 3
  • 12
  • Other Question doesn't resolve my problem. Please open this question – James Apr 07 '21 at 10:32
  • Have you seen [this answer](https://stackoverflow.com/a/46108856/5648954) specifically – Nick Parsons Apr 07 '21 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"));` – Nithish Apr 07 '21 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) – Praveen Danagoudru Apr 07 '21 at 10:43

0 Answers0