0

I'm following this great tutorial for adding a property (or not) to an object based on a condition.

For example:

{  id: 'some-id',  ...(true && { optionalField: 'something'})}

But in my case I have an object which looks like this:

enter image description here

What I'd like to do is if 12-AM property exists, don't overwrite it, add to its property which is a key called message which is an array

And if it doesn't add a new time key i.e. 1230-AM to April-9-2020

This is what I have now:

{
  ...dayInfoInChild,
  [currentDate]: { /* April-9-2020 */
    [timeOfDayAndHour]: { /* 12-AM */
      message: textValueContainer, ['foo']
    },
  },
}

But alas it doesn't add it overwrites...

Any help would be appreciated.

RobG
  • 142,382
  • 31
  • 172
  • 209
Antonio Pavicevac-Ortiz
  • 7,239
  • 17
  • 68
  • 141
  • 8
    I would say _"stop trying to be fancy"_ – Phil Apr 23 '20 at 03:57
  • Just use normal code like and `if` test to express your logic and control things yourself. – jfriend00 Apr 23 '20 at 04:01
  • Do not use images of objects, supply sample data and code as text, along with actual and expected results. – RobG Apr 23 '20 at 04:34
  • If you really _must_, you can use something like immutability-helper https://github.com/kolodny/immutability-helper (or write your similar equivalent). But ya.. `var m = dayInfoInChild[currentDate][timeOfDayAndHour]; m.message && m.message=m.message.concat(textValueContainer,['foo'])` (not sure if this is what you're actually trying to do), why make life difficult for yourself? – user120242 Apr 23 '20 at 04:48
  • @user120242 Thanks for the helper library! I’ll check it out. Just a question, IAH is my present syntax bad? am I missing something? Figured it’s not too out of the realm...”got a nested object, that’s a property of a top level/parent object, how do I add another prop?” – Antonio Pavicevac-Ortiz Apr 23 '20 at 04:54
  • Dunno, I guess some syntactic sugar for doing that could be nice. It's just kind of overkill and probably harder to grok than just doing it normally. Expecting a bit much from the language. I mean ya, `immutability-helper` is better than some alternatives esp. for working with immutable objects (although you might be better off using Immutable.JS). But it'd be kind of weird to expect the (Javascript) language to have that kind of syntax built-in. – user120242 Apr 23 '20 at 05:00

1 Answers1

2

Don't use object literal syntax for this. Object literals is for when you want to create a new object. Instead, manipulate the object:

// Checking existence:
if (obj[currentDate] && obj[currentDate][timeOfDayAndHour]) {
    console.log('exist');
}

// Checking if message exist:
if (obj[currentDate][timeOfDayAndHour].message) {
    console.log('exist');
}

// Adding message array:
obj[currentDate][timeOfDayAndHour].message = [];

// Adding message array with message:
obj[currentDate][timeOfDayAndHour].message = ['foo'];

// Adding message:
obj[currentDate][timeOfDayAndHour].message.push('foo');

Now, using the operations above you can implement your logic. I don't know what your exact logic is but we can demonstrate one implementation. The following is an example of how to add a message only if the date exist but does not matter if the time-of-day or message array exist (it will auto-create them):

function addMessageToDateAutoCreateEverythingElse (obj, date, time, txt) {
  let dateObj = obj[date]

  // Checking date exist:
  if (dateObj) {
    let timeObj = dateObj[time];

    // Auto-create time if not exist
    if (!timeObj) {
      timeObj = {};
      dateObj[time] = timeObj;
    }

    // Auto-create message array if not exist
    if (!timeObj.message) {
      timeObj.message = [];
    }

    // Add new message
    timeObj.message.push(txt);
  }
}  

This is only one logic flow. You can implement any logic you like to add anything to any object.

slebetman
  • 109,858
  • 19
  • 140
  • 171