1

I have an object array. I just want to add one more object "WITH" an index. I'm not sure how it's done.

This is what I've tried:

//RAW DATA:
this.fetchedData = [{
  "startMinute": 0, //Remove
  "annotation": "All Day",
  "twelveHourFormat": "12am-11:59pm", //Remove
  "timeRange": "00:00-23:59",
  "endMinute": 1439 //Remove
}, {
  "startMinute": 300, //Remove
  "annotation": "Morning",
  "twelveHourFormat": "5am-8:59am", //Remove
  "timeRange": "05:00-08:59",
  "endMinute": 539 //Remove
}]

var newTimeRanges = [];
var temp = []; //Final array(assuming i need one)

newTimeRanges = _.each(this.fetchedData, function(time) {
  delete time.startMinute;
  delete time.twelveHourFormat;
  delete time.endMinute;
});

//DATA AFTER REMOVAL

newTimeRanges = {
  0: {
    annotation: "All Day",
    timeRange: "00:00-23:59"
  },
  1: {
    annotation: "Evening",
    timeRange: "16:00-18:00"
  }
}

//DATA TO BE MERGED
var dirtyData = [{
  "timeRange": "3am-6am",
  "annotation": earlymorning
}];

//Essentially (timeRange+DirtyData)


//Expected Output to be sent for backend
{
  "times": [{
      "annotation": "All Day",
      "timeRange": "00:00-23:59"
    }, {
      "annotation": "Morning",
      "timeRange": "05:00-08:59"
    },
    //Add my new Data here
  ]
}

Now, I want to add or remove another object to that array.

temp.push(newTimeRanges);
temp.push(dirtyData);

This does not merge the records. Instead creates two objects one object and another array.

How can I merge. newTimeRanges with DirtyData. (Like I want 3: "Object")

Is there any efficient way of doing this?

Thanks

11
  • why not temp=newTimeRanges; temp.push(dirtyData[0]);//or loop through it Commented Aug 26, 2016 at 18:42
  • 1
    please add the raw data and the wanted result, like an object literal. Commented Aug 26, 2016 at 18:42
  • newTimeRanges started as an array, but when you logged it it became an object. How did you populate it? Commented Aug 26, 2016 at 18:45
  • Why are you initializing newTimeRanges with an empty array if you're just going to immediately replace it with an object? Commented Aug 26, 2016 at 18:52
  • @Barmar when I used underscoreJS loop to go through the loop and remove few things that converted to object. I had to delete bunch of stuff and store it back. Now I want to append to the newly created object. Commented Aug 26, 2016 at 18:54

2 Answers 2

2

You need to push each property of newTimeRanges separately onto the temp array. Then you can concatenate dirtyData.

$.each(newTimeRanges, function(key, obj) {
    temp.push(obj);
}
temp = temp.concat(dirtyData);

Or you could have made newTimeRanges an array instead of an object, and then you could write:

newTimeRanges = [
    {
        annotation:"All Day",
        timeRange:"00:00-23:59"},
    {
        annotation:"Evening",
        timeRange:"16:00-18:00"}
];

temp = newTimeRanges.concat(dirtyData);
Sign up to request clarification or add additional context in comments.

Comments

1

If you want to merge 2 arrays into a third array you must use concat, like this:

var temp = newTimeRanges.concat(dirtyData);

See: Array.prototype.concat()

Changing your code to work:

//RAW DATA:
var fetchedData = [{
  "startMinute": 0, //Remove
  "annotation": "All Day",
  "twelveHourFormat": "12am-11:59pm", //Remove
  "timeRange": "00:00-23:59",
  "endMinute": 1439 //Remove
}, {
  "startMinute": 300, //Remove
  "annotation": "Morning",
  "twelveHourFormat": "5am-8:59am", //Remove
  "timeRange": "05:00-08:59",
  "endMinute": 539 //Remove
}];

fetchedData.forEach(function(time) {
  delete time.startMinute;
  delete time.twelveHourFormat;
  delete time.endMinute;
});

console.log(fetchedData);

//DATA TO BE MERGED
var dirtyData = [{
  "timeRange": "3am-6am",
  "annotation": "earlymorning"
}];

var temp = fetchedData.concat(dirtyData);

console.log(temp);

5 Comments

@Barmar The question is about arrays... Or it should be.
It should be, but read the question. He's replacing the array with an object that happens to have numeric property names. But it's not an array.
Yeah, he is changing it as we speak, so I'm sticking to the title, since it seems that he is just lost with the code.
I mean, it seems that he can accomplish what he wants using arrays.
Thanks guys for your help. I got it to work! Sorry, If i confused you guys!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.