从Javascript数组对象中删除记录

时间:2022-12-29 21:17:47

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.

我有一个java脚本数组对象,我想从这个对象的特定索引中删除条目,我有一个逗号分隔的索引字符串。问题是,当我使用splice array索引删除它时,其他索引的对象没有被删除。

    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.

请帮帮我。如何完成这项工作。

6 个解决方案

#1


1  

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"] 

#2


1  

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

您可以在循环中剪切后递减k迭代器:

k--;

#3


1  

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(', '))

#4


1  

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.

另外,请不要在每次循环迭代中执行.split()操作——对于一个包含4个数字的字符串来说,效率低下可能不会有太大的影响,但它会使代码变得有点混乱,原则上它有点恶心。

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:

如果有可能删除的条件strig可能不会被排序,请在分割后添加.sort():

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

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

…在这种情况下,您不需要向后循环。

#5


1  

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);

#6


-1  

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);
}

#1


1  

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"] 

#2


1  

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

您可以在循环中剪切后递减k迭代器:

k--;

#3


1  

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(', '))

#4


1  

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.

另外,请不要在每次循环迭代中执行.split()操作——对于一个包含4个数字的字符串来说,效率低下可能不会有太大的影响,但它会使代码变得有点混乱,原则上它有点恶心。

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:

如果有可能删除的条件strig可能不会被排序,请在分割后添加.sort():

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

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

…在这种情况下,您不需要向后循环。

#5


1  

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);

#6


-1  

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);
}