0

EDIT : Included more details

Hi I have a Object Array in jQuery which looks like this,

enter image description here

My question is how can I delete a record from that object array by columnheader as parameter. I know there is this

var result = $.grep(records, function(e){ return e.columnheader == currentheader; });

but grep i only used to check if there's a matching data based on currentheader that i passed in. What if I want to delete?

I want to delete a record on the fly as I am looping in that object array, lets I have. data contains all object arrays that is shown in the image.

$.each(data, function(key,value) {
   // Let's say I want the code here to delete a record in the current object array that I'm looping into.
});

Thanks

jackhammer013
  • 2,174
  • 5
  • 35
  • 77
  • possible duplicate of [How to remove specifc value from array using jQuery](http://stackoverflow.com/questions/3596089/how-to-remove-specifc-value-from-array-using-jquery) – Raghavendra Aug 31 '15 at 08:12
  • you can use jQuery `filter` to remove elements from array – J Santosh Aug 31 '15 at 08:13

2 Answers2

4

You can use filter()

The filter() method creates a new array with all elements that pass the test implemented by the provided function.

arr = arr.filter(function(e) {
    return e.columnheader !== currentheader;
});

Demo

var arr = [{
  name: 'John Skeet',
  rank: 1
}, {
  name: 'T.J.Crowder',
  rank: 10
}];

console.log(arr);

arr = arr.filter(function(e) {
  return e.rank !== 10
});

console.log(arr);

UPDATE

I want the code here to delete a record in the current object array that I'm looping into

Changing a property from object in array.

var arr = [{
  name: 'John Skeet',
  rank: 1
}, {
  name: 'T.J.Crowder',
  rank: 10
}];


$.each(arr, function(index, obj) {
  if (obj.rank === 10) {
    arr[index].rank = 9;
  }
});

console.log(arr);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
Tushar
  • 78,625
  • 15
  • 134
  • 154
  • No sir, I don't need to create a new array, I just want to delete a record from an existing object array. – jackhammer013 Aug 31 '15 at 08:11
  • @JoeneFloresca You can assign the result of filtering to the same array, by doing this you're removing an element from arrary. I've updated answer, check the demo – Tushar Aug 31 '15 at 08:14
  • I revise my question Sir, it doesn't work for me. You can check my question again for additional details. – jackhammer013 Aug 31 '15 at 08:37
  • Thanks this works for my purpose and I accept it as answer. However s @Emmanouil Chountasis , I agree it is bad practice to delete a record in a variable you currently looping for it might affect the iterations so I have to change my logic. thanks :) – jackhammer013 Aug 31 '15 at 09:16
0

You can use the JavaScript splice method to do it. First find the index of your object in the array then use the method like that :

your_array.splice(obj_index,0);

EDIT

The easy way but not optimized is to use a for loop to get the index, a better solution is to use linq.js to get the object, then your_array.indexOf(your_obj);

EDIT 2

You can download linq.js here Linq.js

You can use it like this:

function DelteObjFromArray(your_value){
   var objToDelete = Enumerable.From(your_array).Where(function (x) { return x.your_property == your_value; }).FirstOrDefault();

   var objIndex = your_array.indexOf(objToDelete);
   your_array.splice(objIndex,1);
}