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

2

2 Answers 2

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>

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

4 Comments

No sir, I don't need to create a new array, I just want to delete a record from an existing object array.
@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
I revise my question Sir, it doesn't work for me. You can check my question again for additional details.
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 :)
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);
}

5 Comments

First find the index of your object in the array - I think that's the question
No. I want to delete the object in the array based on the columnheader without knowing the index.
I updated my answer. Sorry but I do not know a way to delete an object without the index.
I revise my question Sir, it doesn't work for me. You can check my question again for additional details.
It is a bad practice to delete in a loop, possible the code will fail because the iterator will show a wrong position. I will update my answer in a while.

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.