1

I have below javascript object

temp=[
{'id': 0, 'name': 'Housing', 'value': 3}, 
{'id': 1, 'name': 'Bank', 'value': 8}, 
{'id': 2, 'name': 'Entertainment', 'value': 3},
{'id': 3, 'name': 'Restaurant', 'value': 3},
{'id': 4, 'name': 'Groceries', 'value': 7}
]

I would like to remove a row based on the name field.

Example: if I invoke the function with the name 'Entertainment', I wanted that particular row to be removed and readjust ids (0,1,2,3...) All other rows should be there as is.

After the function, temp should be

temp=[
{'id': 0, 'name': 'Housing', 'value': 3}, 
{'id': 1, 'name': 'Bank', 'value': 8}, 
{'id': 2, 'name': 'Restaurant', 'value': 3},
{'id': 3, 'name': 'Groceries', 'value': 7}
]

How can I do it ?

I tried below:

function realign(tagrm)
{
                   temp.each(function (i, elem) { 
                    temp[i] = { 
                        if(temp[i][name]==tagrm)
                        delete temp[i]              
                      }
}

How can i get that particular row can be removed and other rows id's are readjusted to 0,1,2,3... ?

Satheesh Panduga
  • 818
  • 2
  • 13
  • 32

4 Answers4

1

You shouldn't use a regular loop to remove items, because it will mess up the length property of the array and thus lead the for-loop to try to delete properties that don't exist anymore.

You can use Array.prototype.filter instead to remove items from an array and return a new copy. If the condition is true, the entry is kept, otherwise it is removed.

Then you can use Array.prototype.map to reassign your id (Even though @FelixKling has a good point, that you might not need an extra id at all):

function realign(temp, tagrm) {

  return temp.filter(function (item) { 
    return item.name != tagrm;    
  }).map(function (item, index) {
    item.id = index;
    return item;
  });

}

Which you could then use as:

var temp=[
  {'id': 0, 'name': 'Housing', 'value': 3}, 
  {'id': 1, 'name': 'Bank', 'value': 8}, 
  {'id': 2, 'name': 'Entertainment', 'value': 3},
  {'id': 3, 'name': 'Restaurant', 'value': 3},
  {'id': 4, 'name': 'Groceries', 'value': 7}
];

temp = realign(temp, 'Entertainment'); // Removes 'Entertainment'

console.log(temp);
nils
  • 25,734
  • 5
  • 70
  • 79
1

Try following

var temp = [
   {'id': 0, 'name': 'Housing', 'value': 3}, 
   {'id': 1, 'name': 'Bank', 'value': 8}, 
   {'id': 2, 'name': 'Entertainment', 'value': 3},
   {'id': 3, 'name': 'Restaurant', 'value': 3},
   {'id': 4, 'name': 'Groceries', 'value': 7}
]

function realign(tagrm) {
    var counter = 0;

    // filters the result by removing any object with the name passes
    temp = temp.filter(function (item) { 
        return item.name != tagrm;      
     });

    // reset the id
    temp = temp.map(function(item){
     item.id = counter++;
      return item;
    });
}

realign('Entertainment');

console.log(temp);
Nikhil Aggarwal
  • 28,197
  • 4
  • 43
  • 59
1

Try using Array.prototype.splice() , Array.prototype.forEach() , recursion

var match = "Entertainment";

function filter(arr, m, i) {
  if (i < arr.length) {
    if (arr[i].name === m) {
      arr.splice(i, 1);
      arr.forEach(function(val, index) {
        val.id = index
      });
      return arr
    } else {
      return filter(arr, m, i + 1)
    }
  } else {
      return m + " not found in array"
  }
}

filter(temp, match, 0)

var temp = [{
  'id': 0,
  'name': 'Housing',
  'value': 3
}, {
  'id': 1,
  'name': 'Bank',
  'value': 8
}, {
  'id': 2,
  'name': 'Entertainment',
  'value': 3
}, {
  'id': 3,
  'name': 'Restaurant',
  'value': 3
}, {
  'id': 4,
  'name': 'Groceries',
  'value': 7
}]

var match = "Entertainment";

function filter(arr, m, i) {
  if (i < arr.length) {
    if (arr[i].name === m) {
      arr.splice(i, 1);
      arr.forEach(function(val, index) {
        val.id = index
      });
      return arr
    } else {
      return filter(arr, m, i + 1)
    }
  } else {
      return m + " not found in array"
  }
}

console.log(filter(temp, match, 0), JSON.stringify(temp, null, 2))
guest271314
  • 1
  • 15
  • 104
  • 177
0

Another appoach is to use:

jQuery.grep() : 
Finds the elements of an array which satisfy a filter function.
The original array is not affected.

So the code will be:

temp = $.grep(temp, function(el, idx) {return el.name == "Entertainment"}, true);

gaetanoM
  • 41,594
  • 6
  • 42
  • 61