Is it possible to dynamically set value to javascript object. For example i have the following array of objects:
var result = [
{id: 1,color: ['black']},
{id: 1,color: ['white']},
{id: 1,color: ['green']}
];
And when creating another one i want to set all values color
from array result to the new object. Basically i want it to looks like this:
{id:1, colors: ['black', 'white', 'green']}
I tried something like this:
var result = [
{id: 1,color: ['black']},
{id: 1,color: ['white']},
{id: 1,color: ['green']}
];
var object1 = {
id: 1,
colors: function(){
for(i = 1; i < result.length; i++) {
this.colors.push(result[i].color);
}
}
};
but it doesn't work and basically i understand why, i am just looking for workaround solution. I am pretty new to javascript so i need some suggestions. Any ideas?