0

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

4
  • try ... localdata.push( ... Commented Apr 29, 2014 at 13:27
  • @johnSmith He wants to add it to an object, so he should actually do localdata[0].c_Address = "Address for Mike" (judging from his screenshot the other keys are also prefixed by "c_"). Commented Apr 29, 2014 at 13:33
  • hi john and Daniël but, I don't want this new create index,I want to add in old index data and I do not have my static address line , I want to create dynamic key value .. sample image i.hizliresim.com/dj3OLn.png Commented Apr 29, 2014 at 13:37
  • @user3575477 I know, my code would not create a new index, it would add a property c_Address to an existing localdata entry. But the answer already showed you how to do it, using your dynamicColumns :). Commented Apr 29, 2014 at 13:58

1 Answer 1

2

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;
Sign up to request clarification or add additional context in comments.

Comments

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.