简体   繁体   English

使用ID和Javascript从数组中删除项目

[英]Remove items from array using id with Javascript

I have a function like this: pickListSelect array is has all id (numbers) to delete objects in source array , and target array it is to push elements deleted from source array . 我有这样的功能: pickListSelect array具有删除source array对象的所有id(数字),而target array则是推送从source array删除的元素。

    function copy(pickListSelect, source, target) {

         var i, id;

         for (i = 0; i < pickListSelect.length; i++) {
              id = pickListSelect[i];              
              source.splice(id,1);
         }
         pickListSelect = [];
   }

So what I need is delete specific object from source arra y. 所以我需要从source arra删除特定对象。 I tried with that code but for example if I need to delete object with id=5, it only deleted item 5 from the list. 我尝试使用该代码,但是例如,如果我需要删除id = 5的对象,则它只会从列表中删除项目5。

The structure of source array is this: 源数组的结构是这样的:

    [Object, Object, Object, Object, Object, Object, Object, Object, Object]
    0:Object
    plantId:1
    plantName:"Plant 1"

...the rest of others are similar object

You need to find plant in your source by plantId first, and then delete it from original array and push to target. 您需要首先通过plantIdsource找到植物,然后将其从原始数组中删除并推送到目标。 Open console and it should log deleted plants: 打开控制台,它将记录已删除的植物:

 var plants = [ { plantId: 1, plantName: 'plant 1' }, { plantId: 2, plantName: 'plant 2' }, { plantId: 3, plantName: 'plant 3' }, { plantId: 4, plantName: 'plant 4' } ]; function copy(pickListSelect, source, target) { var i, id, el; for (i = 0; i < pickListSelect.length; i++) { id = pickListSelect[i]; el = findPlant(source, id); source.splice(source.indexOf(el), 1); target.push(el); } } function findPlant (arr, id) { return arr.filter(function (plant) { return plant.plantId == id })[0] } var test = []; copy([2,3], plants, test); console.log(test); 

When you use .splice you need to pass in the start index at which to splice and the amount of items to splice, try this: 使用.splice时,您需要传递用于拼接的起始索引以及要拼接的项目数量,请尝试以下操作:

source.splice(i,1); // i is your starting index here


array.splice(start, deleteCount[, item1[, item2[, ...]]])

MDN on .splice .splice上的MDN

Now in your actual code you need to check to see if the id matches and then splice using the above code: 现在,在您的实际代码中,您需要检查id是否匹配, 然后使用上面的代码进行拼接:

function copy(pickListSelect, source, target) {

     var i, id;

     for (i = 0; i < pickListSelect.length; i++) {
          if (pickListSelect[i].id === someId) {              
            source.splice(i,1);
          }
     }
     pickListSelect = [];
 }

You're not looking up the index of the source array with a matching id. 您没有在查找具有匹配ID的源数组的索引。 It might be better to do something like this. 做这样的事情可能会更好。

var idsToRemove = {};

// build an object of ids to remove (effectively a hashset)
for (var i = 0; i < pickSelectList.length; i++) {
    idsToRemove[pickSelectList[i]] = true;
}

// loop through the source array to find any objects with ids to remove
for (var j = 0; j < source.length; j++) {
    if (source[j].plantId in idsToRemove) {
        target.push(source.splice(j, 1));
    }
}

You can take a look at this fiddler here . 你可以在这里看看这个提琴手。 I have used underscore.js to find the correct element from source and move it to the target array. 我已经使用underscore.js从源代码中找到正确的元素,并将其移动到目标数组。

var copy = function(pickListSelect, source, target) {
    for (i = 0; i < pickListSelect.length; i++) {
      id = pickListSelect[i];
      var deleteIndex = _.findIndex(source, {Id: id});
            var deletedItem = source.splice(deleteIndex, 1);
            target.push(deletedItem[0])
    }
        pickListSelect = [];
    return target;
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM