1

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',
            },
        ];
3
  • You can map the array and assign the index: x = x.map((item, index) => Object.assign(item, { index: index % 2 })); jsfiddle.net/k3ghxzeu/1 Commented Oct 13, 2018 at 9:11
  • do you need the modulo value for display? if so, you could use CSS with nth-child or nth-of-type. Commented Oct 13, 2018 at 9:13
  • Indeed, it really sounds like you need to use it for display purposes. If so, you don't need to track the indexin this way, as Nina mentioned above Commented Oct 13, 2018 at 9:14

5 Answers 5

3

You can use .forEach() and modulus operator like this:

var x = [
  {"place": 1, "name": 'Steve'},
  {"place": 4, "name": 'Ann'},
  {"place": 9, "name": 'John'}
];

x.forEach((o, i) => (o.index = i % 2));

console.log(x);

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

Comments

2

You can just loop through the array and add an index value to each object. They can be consecutive or alternate as shown below:

var x = [ { "place": 1, "name": 'Steve' },
          { "place": 4, "name": 'Ann' },
          { "place": 9, "name": 'John' }
        ];
        
for (let i = 0; i < x.length; i++) {
    x[i].index = i;
}
    
console.log(x);
    
for (let i = 0; i < x.length; i++) {
    x[i].index = i % 2;
}
    
console.log(x);

1 Comment

Thank You to Best community in the World. I have so many solutions ....
2

You only need to specify the new attribute for every object in the array:

for (var i = 0; i < x.length; i++) {
        x['index']=i;    
}

If you would like to alternate 0 and 1 as index values, you should use the mod operator and check if the value equals to 0:

for (var i = 0; i < x.length; i++) {
        x['index']=(i % 2 == 0);    
}

Comments

0

first of all you array of objects so u need to run over the array and add each object new "index" property.

x.forEach(function(obj, index){
   obj['Index'] = index;
})

now for if u want 0-1-0-1...

id do smth like htis:

var count = 0;

    x.forEach(function(obj){
      if(count==0){
        obj['Index'] = --count;
      }else{
       obj['Index'] = ++count;
      }
    })

Comments

0

you could say:

x.forEach(function(val, index) {
    //depending on the index you need
    val.index = index // or = index % 2 
})

And, x will have that additional key index with values you need. Let me know if that does not help.

Comments

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.