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
radek.
3781 gold badge3 silver badges16 bronze badges
2 Answers 2
If I understand you correctly, you are after something like this maybe?
var obj = {};
...
obj["count"+i] = ...
answered Oct 7, 2015 at 9:23
bgse
8,6672 gold badges43 silver badges43 bronze badges
You Simply use concat Function
var obj=data.concat(data2,data3);
answered Oct 7, 2015 at 9:24
Akhilesh Singh
1,7342 gold badges19 silver badges35 bronze badges
1 Comment
SuperBiasedMan
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.
lang-js