rahul
rahul

Reputation: 7663

Delete Record From Javascript Array Object

i have a java script array object and i want to delete items from a specific index in that object, i have a comma separated string of that indexes. problem is that when i delete it using splice array indexes got changed, and other indexes's object not got deleted.

    var DeletedConditions="3, 5, 19, 50";

    for (var k = 0; k < DeletedConditions.split(", ").length; k++) {
         ConditionObject.splice(DeletedConditions.split(", ")[k], 1);
    }

DeletedConditions string can be anything.

please help me out. how to get this done.

Upvotes: 2

Views: 695

Answers (6)

Madhavi Mangapati
Madhavi Mangapati

Reputation: 37

var fruits = new Array("apple", "banana", "grapes", "oranges","mosambi","aaa","bbb","ccc"); 

var DeletedConditions="1,3,4,5";

var indexArray = new Array;

indexArray = DeletedConditions.split(",");

for (var i = 0; i < indexArray.length; i++) {

   fruits.splice(indexArray[i], 1);
}

Upvotes: -1

Lee
Lee

Reputation: 13542

It might be easiest to copy the original array, omitting the deleted items in the process. Something like this would do the trick...

var DeletedConditions="3, 5, 19, 50";
DeletedConditions = DeletedConditions.split(', ');
var newConditionObject = [];
for(var k = 0; k < ConditionObject.length; ++k) {
  if(DeletedConditions.indexOf(k) !== -1) { continue; }
  newConditionObject.push(ConditionObject[k]);
}

// result is in `newConditionObject`
console.log(newConditionObject);

Upvotes: 1

nnnnnn
nnnnnn

Reputation: 150010

Removing an item from the beginning of the array shuffles the later elements up and changes their indices, as you've observed. But if you go through the list of items to remove backwards then it will remove the later elements first so the indices will still be correct for the elements closer to the beginning of the array.

Also, please don't do the .split() operation on every loop iteration - the inefficiency might not make much difference on a string with four numbers in it, but it makes the code kind of messy and on principle it is just kind of yucky.

var DeletedConditions="3, 5, 19, 50",
    delCondArr = DeletedConditions.split();

for (var k = delCondArr.length - 1; k >= 0; k--) {
     ConditionObject.splice(delCondArr[k], 1);
}

If there's a possibility that the DeletedConditions strig might not be ordered just add a .sort() after you split it:

delCondArr = DeletedConditions.split().sort(function(a,b){return a-b;});

...in which case you don't need to loop backwards.

Upvotes: 1

Joseph
Joseph

Reputation: 119837

First of all, I suggest you officially turn the indexes into a formal array. Having a string as an index reference, you are prone to missing a split shall there be a case where the values are not separated by ,

Then the code:

var content = ['foo', 'bar', 'baz', 'bam', 'dom', 'zok'],
    deleteIndexes = [5, 1, 3],//should delete zok, bar, bam
    i;

//sort from least to greatest: [1, 3, 5]
deleteIndexes.sort(function(a, b) {
    return a - b;
});

//we loop backwards (performance enhancement)
//now we loop from greatest to least
//we now splice from greatest to least 
//to avoid altering the indexes of the content as we splice
for (i = deleteIndexes.length; i-- > 0;) {
    content.splice(deleteIndexes[i],1);
}

console.log(content); //["foo", "baz", "dom"] 

Upvotes: 1

Niks Jain
Niks Jain

Reputation: 1647

var DeletedConditions="3, 5, 19, 50";
var list = DeletedConditions.split(", ")
for (var k = 0; k < list.length; k++) {
    // using splice here   
    list.splice(k,1); 
     k--;
}

console.log(list.join(', '))

Upvotes: 1

user1796666
user1796666

Reputation:

You can always decrement the k iterator after splicing inside the loop:

k--;

Upvotes: 1

Related Questions