I'm wondering, how to do task like this: I have some array:
var x = [
{
"place": 1,
"name": 'Steve',
},
{
"place": 4,
"name": 'Ann',
},
{
"place": 9,
"name": 'John',
},
];
Now i would like to add new value, that will keep index number, to have the result as:
var x = [
{
'index':0,
"place": 1,
"name": 'Steve',
},
{
'index':1,
"place": 4,
"name": 'Ann',
},
{
'index':2,
"place": 9,
"name": 'John',
},
];
I've tried this, but seems it's bad solution and not working:
for (var i = 0; i < x.length; i++) {
arr.push(x['index'],i);
}
Thanks for help with this, and i don't want to be greddy, but if would be also possible to help with the following it would be extra: Is it possible to have "index" values as 0 or 1, so if for example i have 5 items in array, is there any way to do check in the loop using modulo operaror and assign index value as 0,1,0,1,0? Best regards.
var x = [
{
'index':0,
"place": 1,
"name": 'Steve',
},
{
'index':1,
"place": 4,
"name": 'Ann',
},
{
'index':0,
"place": 9,
"name": 'John',
},
{
'index':1,
"place": 9,
"name": 'John',
},
{
'index':0,
"place": 9,
"name": 'John',
},
];
x = x.map((item, index) => Object.assign(item, { index: index % 2 }));jsfiddle.net/k3ghxzeu/1nth-childornth-of-type.