Ali Foroughi
Ali Foroughi

Reputation: 4619

add to array of a complext type in javascript

look at this code please :

var data = {                                
    "cell": [
    "1",
    "text1",
    "user1",
    "date1"
]};

this is an array contains just 1 element and now I need a code to set the following value into data variable:

data = {                                
    "cell": [
    "1",
    "text1",
    "user1",
    "date1"
    ]
},
{                                
    "cell": [
    "2",
    "text2",
    "user2",
    "date2"
]};

I use the following code , but it does not work.

var data = {                                
    "cell": [
    "1",
    "text1",
    "user1",
    "date1"
    ]
};

data += {                                
    "cell": [
    "2",
    "text2",
    "user2",
    "date2"
    ]
};

Note , I want to ADD to this array.i need a code to add another element to data variable.

Upvotes: 0

Views: 198

Answers (6)

Quentin
Quentin

Reputation: 944054

data is an object. It isn't an array. If you want to add to it, you need to assign a value to a new property on it.

var data = {
    "cell": ["1", "text1", "user1", "date1"]
};
data.cell2 = ["2", "text2", "user2", "date2"];

Perhaps you want data to be an array of objects instead?

var data = [{
    "cell": ["1", "text1", "user1", "date1"]
}];

data.push({
    "cell": ["2", "text2", "user2", "date2"]
});

Upvotes: 1

Declan Cook
Declan Cook

Reputation: 6126

If you look at your data object from

var data = {                                
    "cell": [
    "1",
    "text1",
    "user1",
    "date1"
]};

you will see that it has a property cell that is an array with 4 elements. If that is correct if you want to add another cell you can make the data object an array like so:

var data = [{                                
    "cell": [
    "1",
    "text1",
    "user1",
    "date1"
]}]; 

data.push( {"cell": ["2","text2","user2","date2"]});

or you can add another cell property to the data object but it will need a different key or you will override the current cell property.

data['cell2'] = [
"2",
"text2",
"user2",
"date2"
];

Upvotes: 1

Rohit
Rohit

Reputation: 35

You should set the data like this

data = [{                                
       "cell": [
       "1",
       "text1",
       "user1",
       "date1"
       ]
   },
   {                                
       "cell": [
       "2",
       "text2",
       "user2",
       "date2"
       ]
   }];

And to add a new cell to the data, do it like this

var dt = {                                
    "cell": [
    "3",
    "text3",
    "user3",
    "date3"
    ]
};
data.splice(data.length,0,dt);

Upvotes: -1

RoToRa
RoToRa

Reputation: 38431

Two things:

1) This isn't an array, it's a object.

2) You can't have the same key twice in an object.

To add a key to a object use:

data['cell2'] = [
    "2",
    "text2",
    "user2",
    "date2"
    ];

Upvotes: 1

Andrey Selitsky
Andrey Selitsky

Reputation: 2604

Incorrect syntax. Should be:

var data = [{                                
    "cell": [
    "1",
    "text1",
    "user1",
    "date1"
]}]; 

data.push( {.....your new object....});

Upvotes: 1

Sharon
Sharon

Reputation: 919

Those are not arrays, they're object literals. So what you want to do is merge two objects together.

See How can I merge properties of two JavaScript objects dynamically?:

for (var attrname in obj2) { obj1[attrname] = obj2[attrname]; }

If you're using a framework that craps all over your prototypes then you have to get fancier with checks like hasOwnProperty, but that code will work for 99% of cases.

Example function:

/**
 * Overwrites obj1's values with obj2's and adds obj2's if non existent in obj1
 * @param obj1
 * @param obj2
 * @returns obj3 a new object based on obj1 and obj2
 */
function merge_options(obj1,obj2){
  var obj3 = {};
  for (var attrname in obj1) { obj3[attrname] = obj1[attrname]; }
  for (var attrname in obj2) { obj3[attrname] = obj2[attrname]; }
  return obj3;
}

If you're using jQuery, you could use $.extend for this.

Upvotes: 2

Related Questions