Reputation: 31
How to add array new key value ( javascript ) ?
sample image http://i.hizliresim.com/dj3OLn.png
example this my array
source.localdata[0].c_Id="2d5955ee-415b-470a-8700-3ae65ee122db"
Name ="mike"
LastName="jonas"
source.localdata[1].c_C_ID"b851c285-035f-4a51-8237-1e3c3528e089"
Name ="richard"
LastName="gere"
and I want to enter key and value data
source.localdata[0].Address = "mike for address"
source.localdata[1].Address = "richard for address"
..more
output
source.localdata[0].c_Id="2d5955ee-415b-470a-8700-3ae65ee122db"
Name ="mike"
LastName="jonas"
Address = "mike for address"
source.localdata[1].c_C_ID"b851c285-035f-4a51-8237-1e3c3528e089"
Name ="richard"
LastName="gere"
Address = "richard for address"
I wrote this code but I am getting error JavaScript runtime error: Object doesn't support property or method 'push'
function dataSourceCompare(newdata) {
for (var i = 0; i < source.localdata.length; i++) {
for (var j = 0; j < newdata.dynamicColumns.length; j++) {
if (crewItems[i].c_C_ID === newdata.dynamicColumns[j].ColumnValue) {
source.localdata[i].push([newdata.dynamicColumns[j].ColumnName, newdata.dynamicColumns[j].ColumnValue]);
}
}
}
}
thank you
Upvotes: 0
Views: 122
Reputation: 174
Array.push() is used to add new elements to an array, and cannot be used on a plain javascript object. What you want to do is add a new propriety to an object, with a dynamic property name, and here's how to do it:
source.localdata[i][newdata.dynamicColumns[j].ColumnName] = newdata.dynamicColumns[j].ColumnValue;
Upvotes: 2