简体   繁体   English

如何使用javascript或jquery从arrayList中删除对象?

[英]How to remove object from arrayList using javascript or jquery?

I have a arrayList which is pushing: 我有一个推的arrayList:

imageList.push({
    docId: docId,
    letterNo: letterNo,
    seqNo: seqNo
});

First the data pushed in my array is docId:1 ,letterNo:1,seqNo:1 which is: 首先,在我的数组中推送的数据是docId:1,letterNo:1,seqNo:1 ,即:

"{"docId":1,"letterNo":1,"seqNo":1}"

Another time the New data I send is docId:2 ,letterNo:1,seqNo:1 which is : 下次我发送的New datadocId:2,letterNo:1,seqNo:1 ,它是:

 "{"docId":2,"letterNo":1,"seqNo":1}"

I want to compare all the old values of arrayList with new values and if all these three property matches,then I need to slice old object from arrayList. 我想将arrayList的所有旧值与新值进行比较,如果这三个属性都匹配,则需要从arrayList中切片旧对象。 For example if i again send "{"docId":1,"letterNo":1,"seqNo":1}" ,then old object should be poped out from arraylist,as it matches with old object and this current object needs to be pushed.So i tried: 例如,如果我再次发送"{"docId":1,"letterNo":1,"seqNo":1}" ,则应从arraylist中弹出旧对象,因为它与旧对象匹配,并且当前对象需要被推。所以我尝试了:

imageList = imageList
    .filter(function (
        obj) {
        return (obj.docId !== docId && obj.letterNo !== letterNo && obj.seqNo !== seqNo);
    });

Though docId of two object 1 and 2 are different,it needs to be pushed,But it is just removing all object from arrayList,though all the three property are not matched. 尽管两个对象1和2的docId是不同的,但需要推送,但这只是从arrayList中删除所有对象,尽管这三个属性都不匹配。

 var newData = { "docId":2, "letterNo":1, "seqNo":1 }; //Solution using filters var insertNewData = false; var imageList = [{ "docId":1, "letterNo":1, "seqNo":1 }, { "docId":2, "letterNo":1, "seqNo":1 }, { "docId":3, "letterNo":1, "seqNo":1 }]; imageList = imageList.filter(function(oldData){ if(oldData["docId"] == newData["docId"] && oldData["letterNo"] == newData["letterNo"] && oldData["seqNo"] == newData["seqNo"]) { insertNewData = true;//a flag return false;//remove old data from array } else return true; }); if(insertNewData) { imageList.push(newData); insertNewData = false; console.log("old data is removed and new data is pushed using filters",imageList); } 

In addition to this, I would like to provide some solutions which will prevent removing the items and again adding the removed items into the array. 除此之外,我想提供一些解决方案,这些解决方案将防止删除项目并将再次删除的项目添加到数组中。 Because you don't need to remove old data if it is similar to new data. 因为如果旧数据与新数据相似,则不需要删除它。 Instead, you need to tell your program not to store the new data if old data already exists. 相反,如果旧数据已经存在,则需要告诉程序不要存储新数据。

//For searching objects in an array, convert objects into strings.
for(var i in imageList)
{
    imageList[i] = JSON.stringify(imageList[i]);
}

//Solution 1: Using Array.includes()
if(!imageList.includes(JSON.stringify(newData)))
    imageList.push(JSON.stringify(newData));

//Solution 2: Using Array.indexOf()
if(imageList.indexOf(JSON.stringify(newData)) == -1)
    imageList.push(JSON.stringify(newData));

Your filter did not work because return (obj.docId !== docId && obj.letterNo !== letterNo && obj.seqNo !== seqNo); 您的筛选器不起作用,因为return (obj.docId !== docId && obj.letterNo !== letterNo && obj.seqNo !== seqNo); will return true. 将返回true。 You need to return false if that condition is satisfied so that the old value will be filtered from your array. 如果满足该条件,则需要返回false,以便将旧值从数组中过滤掉。

this code will give you desired output... 该代码将为您提供所需的输出...

 var imageList = [ { docId: 1, letterNo: 1, seqNo: 1 }, { docId: 2, letterNo: 2, seqNo: 2 }, { docId: 3, letterNo: 3, seqNo: 3 } ]; const someFunction = (imageList = [], docId, letterNo, seqNo) => { let newimageList = imageList .filter(image => { let newValues = [docId, letterNo, seqNo]; let imageListvalues = [...Object.values(image)]; return !(JSON.stringify(newValues) === JSON.stringify(imageListvalues)); }); newimageList.push({ docId: docId, letterNo: letterNo, seqNo: seqNo }); return newimageList; }; var newimageList = [] newimageList = someFunction(imageList, 1, 1, 1); newimageList = someFunction(newimageList, 1, 2, 1); console.log(newimageList); 

var imageList = [];

function checkPush(value) {
  imageList.map((val, index) => {
    if (
      value.docId == val.docId &&
      value.letterNo == val.letterNo &&
      value.seqNo == val.seqNo
    ) {
      imageList.splice(index, 1);
    }
  });
  imageList.push(value);
}
checkPush({ docId: 1, letterNo: 1, seqNo: 1 });
checkPush({ docId: 2, letterNo: 1, seqNo: 1 });
checkPush({ docId: 1, letterNo: 1, seqNo: 1 });
console.log(imageList);

Before adding the new data, you could check and remove the duplicated one using the following code: 在添加新数据之前,您可以使用以下代码检查并删除重复的数据:

function GetDuplicatedIndex(imageArray, newData) {
    return imageArray.findIndex(x => (x.docId == newData.docId && x.letterNo == newData.letterNo && x.seqNo == newData.seqNo));
}

var newData = {"docId":1,"letterNo":1,"seqNo":1}

// find the index of the duplicated data
var dupIndex = GetDuplicatedIndex(imageList, newData);

// remove the duplicated one if exists
if (dupIndex > -1) { imageList.splice(dupIndex, 1); }

// add new data
imageList.push(newData);

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

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