1

I'm trying to build an array of javascript objects based on a variable that also has objects like this:

time #1:

console.log(obj_initial);
//{ evt1 : 'value1', evt2 : 'value2', evt3 : 'value3' }

This variable change theirs properties in the time.

time #2:

console.log(obj_initial);
//{ evtA : 'value9', evtB : 'value8', evtC : 'value7', evtD : 'value6' }

How do I build an array of objects? like this:

obj_final = [
    {
        evt1 : 'value1',
        evt2 : 'value2',
        evt3 : 'value3' },
    {
        evtA : 'value9',
        evtB : 'value8',
        evtC : 'value7',
        evtD : 'value6'},
    {
        ...
    }]

Anyone please.. thanks in advance.

Travis J
  • 81,153
  • 41
  • 202
  • 273
paerguca
  • 11
  • 1
  • 1
    clone the object and put the clone into the array every time you want to save the properties (http://stackoverflow.com/questions/122102/what-is-the-most-efficient-way-to-clone-an-object) – akonsu Jan 27 '15 at 22:41
  • Is the object re-assigned as you show in the second scenario? If it is that will be a new object, and as a result cloning is not necessary. – Travis J Jan 27 '15 at 22:51
  • Yes, the variable changes their properties and the amount of them like the example. – paerguca Jan 27 '15 at 22:57
  • Thanks for answer quickly. I don't know when the variable will change because always do it. Already test the statement but the result was: [{evt1 : 'value1',...(same)}],[{evt1 : 'value1',... (same)},{evtA : 'value9'},...] . Repeat the first object. Please help! – paerguca Jan 28 '15 at 12:42

3 Answers3

2

You could just push each new object in the object_final. Something like this:

object_final.push(obj_initial);

This statement should be called each time the value that point to obj_initial changes.

Christos
  • 53,228
  • 8
  • 76
  • 108
  • Thanks for answer quickly. I don't know when the variable will change because always do it. Already test the statement but the result was: [{evt1 : 'value1',...(same)}],[{evt1 : 'value1',... (same)},{evtA : 'value9'},...] . Repeat the first object. Please help! – paerguca Jan 28 '15 at 12:41
1

jsFiddle Demo

Since you are re-assigning the value of obj_initial there is no need to clone.

var obj_final = [];

var obj_initial = {
    evt1 : 'value1',
    evt2 : 'value2',
    evt3 : 'value3'
};

obj_final.push(obj_initial);

//this assignment uses a new object reference
//and obj_initial is no longer a reference to the object pushed
//into obj_final
obj_initial = {
    evtA : 'value9',
    evtB : 'value8',
    evtC : 'value7',
    evtD : 'value6'
};

obj_final.push(obj_initial);
Travis J
  • 81,153
  • 41
  • 202
  • 273
  • Thanks for answer quickly. I don't know when the variable will change because always do it. Already test the statement but the result was: [{evt1 : 'value1',...(same)}],[{evt1 : 'value1',... (same)},{evtA : 'value9'},...] . Repeat the first object. Please help! – paerguca Jan 28 '15 at 12:42
  • @paerguca - That is a different scenario than you showed in your demo :) – Travis J Jan 28 '15 at 17:23
0

You need to clone the object before adding it to an array if you want to take "snapshots" of its state:

function addToArr(obj, arr) {
  var newObj = {};
  for (var prop in obj) {
    newObj[prop] = obj[prop];
  }
  arr.push(newObj);
}

Every time you want to push the object to the array, just call

addToArr(obj_initial, obj_final);
Daniel Weiner
  • 1,874
  • 12
  • 19