2

How I can create dynamic name of object from my value in array? For example:

This is my code:

var data=[{"column":"1", "count":"1"},{"column":"2", "count":"2"},{"column":"3", "count":"3"}];
var data2=[{"column":"1", "count":"5"},{"column":"2", "count":"2"},{"column":"3", "count":"7"}];
var data3=[{"column":"1", "count":"9"},{"column":"2", "count":"6"},{"column":"3", "count":"1"}];
var obj=[];
for (i=0;i<data.length;++i)
 obj.push({date: data[i].column, count1: data[i].count});
for (i=0;i<data2.length;++i)
 obj[i].count2 = data[2].count;
for (i=0;i<data3.length;++i)
 obj[i].count3 = data3[i].count;
console.log(obj);

and this is link for jsfiddle: https://jsfiddle.net/r4tnewxg/

As you can see in the example is part of "count 1", etc. You can do it in a dynamic way? For example, the for loop and taking advantage of the 'i'? Then the name would be a "count" + i " results: " count1 "," count2 " etc.

asked Oct 7, 2015 at 9:16

2 Answers 2

3

If I understand you correctly, you are after something like this maybe?

var obj = {};
...
obj["count"+i] = ...
answered Oct 7, 2015 at 9:23
Sign up to request clarification or add additional context in comments.

2 Comments

"obj[i].count+i" This is the problem. I can't write 'count+i' for new element from my array
Yes, you'd have to use an object actually.
2

You Simply use concat Function

var obj=data.concat(data2,data3);
answered Oct 7, 2015 at 9:24

1 Comment

Please consider editing your post to add more explanation about what your code does and why it will solve the problem. An answer that mostly just contains code (even if it's working) usually wont help the OP to understand their problem.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.