Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

This is my array object

var item = [
    {index:1, name: 'miraje'},
    {index:2, name: 'alamin'},
    {index:3, name: 'behestee'},
    {index:4, name: 'arif'},
    {index:5, name: 'riad'}
];

when i delete an object like index: 2 , and that time i want to update my index value like ..

   var item = [
            { index: 1, name: 'miraje'},
            { index: 2, name: 'behestee'},
            { index: 3, name: 'arif'},
            { index: 4, name: 'riad'}
         ];
share|improve this question
    
Show code efforts – Shubhranshu 10 hours ago
    
your accepted answer does not match the requirement with looking for index = 2. it uses a known index (1) of the array, which is not given. – Nina Scholz 10 hours ago
up vote 0 down vote accepted

After you remove element you can use forEach() loop to change indexes.

var item = [
{index:1, name: 'miraje'},
{index:2, name: 'alamin'},
{index:3, name: 'behestee'},
{index:4, name: 'arif'},
{index:5, name: 'riad'}
];

item.splice(1, 1)
item.forEach((e, i) => e.index = i + 1)
console.log(item)

share|improve this answer

Just for a variety, without modifying the original array an efficient O(n) approach would also be as follows. We can also provide a range to delete the elements as well...

The range is provided supplied with the array indices, not the object index properties.

var item = [
    {index:1, name: 'miraje'},
    {index:2, name: 'alamin'},
    {index:3, name: 'behestee'},
    {index:4, name: 'arif'},
    {index:5, name: 'riad'}
];
function deleteObjectsFromArray(a,j,k){
  return a.reduceRight((p,c,i) => i >= j && i < k ? p
                                                  : i >= k ? (c.index -= k-j, p[c.index-1] = c, p)
                                                           : (p[c.index-1] = c, p),[]);
}

console.log(deleteObjectsFromArray(item,2,4));

share|improve this answer

Basically, you need to find the item with index, delete it, and to update all following items.

function deleteItem(array, index) {
    var i = 0, found = false;
    while (i < array.length) {
        if (found) {
            --array[i].index;
            ++i;
            continue;
        }
        if (found = array[i].index === index) {
            array.splice(i, 1);
            continue;
        }
        ++i;
    }
}

var items = [{ index: 1, name: 'miraje' }, { index: 2, name: 'alamin' }, { index: 3, name: 'behestee' }, { index: 4, name: 'arif' }, { index: 5, name: 'riad' }];

deleteItem(items, 2);
console.log(items);
.as-console-wrapper { max-height: 100% !important; top: 0; }

share|improve this answer

Remove the object and alter the index property of each object,

DEMO

i=1;
var item = [
{index:1, name: 'miraje'},
{index:2, name: 'alamin'},
{index:3, name: 'behestee'},
{index:4, name: 'arif'},
{index:5, name: 'riad'}
];
console.log(item);
delete item[ 2 ];
console.log(item);
item.forEach(function(obj) {
     obj.index = i;
     debugger;
     i++;
});
console.log(item);

share|improve this answer

use foreach loop and assign the index

item.forEach(function(obj,i){
   obj.index = i+1;
})
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.