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

时间:2019-02-18 09:56:26

标签: javascript jquery arraylist

我有一个push的arrayList:

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

首先,在我的数组中推送的数据是 docId:1,letterNo:1,seqNo:1 ,即:

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

我再次发送的New data docId:2,letterNo:1,seqNo:1 ,即:

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

我想将arrayList的所有旧值与新值进行比较,如果这三个属性都匹配,则需要从arrayList中切片旧对象。例如,如果我再次发送"{"docId":1,"letterNo":1,"seqNo":1}",则应该从arraylist中弹出旧对象,因为它与旧对象匹配,因此需要推送当前对象。所以我尝试了:

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

尽管两个对象1和2的docId不同,但需要推送,但这只是从arrayList中删除所有对象,尽管这三个属性都不匹配。

4 个答案:

答案 0 :(得分:2)

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

除此之外,我想提供一些解决方案,这些解决方案将防止删除项目并将再次删除的项目添加到数组中。因为如果旧数据与新数据相似,则不需要删除它。相反,如果旧数据已经存在,您需要告诉程序不要存储新数据。

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

您的过滤器不起作用,因为 return (obj.docId !== docId && obj.letterNo !== letterNo && obj.seqNo !== seqNo);将返回true。如果满足该条件,则需要返回false,以便从您的数组中过滤掉旧值。

答案 1 :(得分:0)

此代码将为您提供所需的输出...

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

答案 2 :(得分:0)

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

答案 3 :(得分:0)

在添加新数据之前,您可以使用以下代码检查并删除重复的数据:

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