Reputation: 99
I have a arrayList which is pushing:
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}"
Another time the New data
I send is docId:2 ,letterNo:1,seqNo:1 which is :
"{"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. 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:
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.
Upvotes: 1
Views: 1023
Reputation: 551
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);
will return true. You need to return false if that condition is satisfied so that the old value will be filtered from your array.
Upvotes: 2
Reputation: 1138
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);
Upvotes: 0
Reputation: 568
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);
Upvotes: 0
Reputation: 879
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);
Upvotes: 0