0

I defined this:

var data ={};

I want to get somehting like this for each "line" in the object:

{field1: "bananas", field2:'test', field3:111, field4:"23.4", field5:"bob"},
{field1: "fruit", field2:'test again', field3:222, field4:"30", field5:"john"}

I know I can do this to add dynamic and static data:

data['field1']= docType;
    data['field2'] = docRef;
    data['field3'] = "test3";
    data['field4'] = mydynamicdata;
    data['field5'] = "test5";

But how can I add different items lines? This code would only add a item set, right? How can I add a second one?

thank you!

1

1 Answer 1

2

You need an Array of Objects - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array

var data = [];

data.push({field1: "bananas", field2:'test', field3:111, field4:"23.4", field5:"bob"})
data.push({field1: "fruit", field2:'test again', field3:222, field4:"30", field5:"john"})

Then you can make changes like this:

data[0]['field1'] = 'new value'
//or:
data[0].field1 = 'new value'

Where 0 is the index of the item inside of the list. So data[1] would be the second object, and so on...

Sign up to request clarification or add additional context in comments.

2 Comments

hello, This data is to pass to yui datatable. Can it receive that array of objects? By now im passing a data ={};
actually, according to the docs - yuilibrary.com/yui/docs/datatable - the expected format of data is an Array

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.