Loop Through Array Of Objects And Add Them Based On A Property
Solution 1:
functionremoveDuplicates(arr) {
console.log("original length: "+arr.length);
for (var i=0; i<arr.length; i++) {
var listI = arr[i];
loopJ: for (var j=0; j<arr.length; j++) {
var listJ = arr[j];
if (listI === listJ) continue; //Ignore itselffor (var k=listJ.length; k>=0; k--) {
if (JSON.stringify(listJ[k]) !== JSON.stringify(listI[k])) continue loopJ;
}// At this point, their values are equal.
arr.splice(j--, 1);
}
}
console.log("length without duplicates: "+arr.length);
return arr;
}
This is a function I wrote a little while back to remove any duplicate objects in an array. Since you want to apply only to two attributes then all we need to do is remove the 3rd loop and just check those two attributes instead.
Also note, there are better ways to compare objects than JSON.stringify but efficiency wasn't needed for me, I don't know if that's the case for you.
Modified code for your purposes:
functionremoveDuplicates(arr) {
console.log("original length: "+arr.length);
for (var i=0; i<arr.length; i++) {
var listI = arr[i];
loopJ: for (var j=0; j<arr.length; j++) {
var listJ = arr[j];
if (listI === listJ) continue; //Ignore itselfif (JSON.stringify(listJ.KODI) !== JSON.stringify(listI.KODI)) {
continue loopJ;
}
if (JSON.stringify(listJ.KODNJESIA1) !== JSON.stringify(listI.KODNJESIA1)) {
continue loopJ;
}
arr.splice(j--, 1);
}
}
console.log("length without duplicates: "+arr.length);
return arr;
}
I hope this is what you were looking for
Solution 2:
A simple way to filter based on 2 properties is to create a temporary hash object that uses the combined values of those 2 properties as keys.
var data = [{
key1: 'A',
key2: 1
}, {
key1: 'A',
key2: 1
}, {
key1: 'B',
key2: 1
}];
var tmp = {};
var res = data.filter(function(item) {
// use a separator for the values to prevent possible collisions and prevent adding numbers var combined = item.key1 + '|' + item.key2,
isUnique = !tmp[combined];
if (isUnique) {
tmp[combined] = true;
}
return isUnique;
});
console.log(res)
The second part of the question is a bit unclear
Solution 3:
First get unique record based on KODARTIKULLI and add the storage units to that unique record.
vararray = [{
BARKOD:"Pa Detatime",
DETAJIM1:"",
DETAJIM2:"",
DTMODIFIKIM:"2017-10-02T16:06:53.206Z",
KODARTIKULLI:"SD13137",
KODI:"MX02",
KODNJESIA1:"cope",
PERSHKRIMARTIKULLI:"Emporio Armani 4097 5574 71 56",
cmimibaze:0,
gjendje:1,
},{
BARKOD:"Pa Detatime",
DETAJIM1:"",
DETAJIM2:"",
DTMODIFIKIM:"2017-10-02T16:06:53.206Z",
KODARTIKULLI:"SD13137",
KODI:"MX02",
KODNJESIA1:"cope",
PERSHKRIMARTIKULLI:"Emporio Armani 4097 5574 71 56",
cmimibaze:0,
gjendje:1
},{
BARKOD:"Pa Detatime",
DETAJIM1:"",
DETAJIM2:"",
DTMODIFIKIM:"2017-10-02T16:06:53.206Z",
KODARTIKULLI:"SD13137",
KODI:"MX03",
KODNJESIA1:"cope",
PERSHKRIMARTIKULLI:"Emporio Armani 4097 5574 71 56",
cmimibaze:0,
gjendje:1
},
{
BARKOD:"Pa Detatime",
DETAJIM1:"",
DETAJIM2:"",
DTMODIFIKIM:"2017-10-02T16:06:53.206Z",
KODARTIKULLI:"SD13141",
KODI:"MX02",
KODNJESIA1:"cope",
PERSHKRIMARTIKULLI:"Emporio Armani 4097 5574 71 56",
cmimibaze:0,
gjendje:1
}];
Here added new property to storage all the KODI info as array
function addStorage(arr,hash,index,sd){
var item = hash[index];
if(item.SD.indexOf(sd) === -1){
item.SD.push(sd);
}
arr[item.index].StorageUnit = item.SD;
arr[item.index].gjendje = item.SD.length;
}
getUnique method will remove the duplicate based on KODARTIKULLI and add record with different store to unique record
function getUnique(arr) {
var hash = [];
var storageHash = [];
var length = arr.length,index = 0;
for (; index < length;) {
var item = arr[index];
var key = item.KODARTIKULLI;
if (hash[key] !== undefined)
{
addStorage(arr,hash,key,item.KODI);
arr.splice(index, 1);
index--;
length--;
} else {
hash[key] = {
index:index,
SD:[item.KODI]
}
}
index++;
}
return arr;
}
console.log(getUnique(array));
Solution 4:
I think you are looking for array.prototype.reduce, that will allow you to apply a callback function to your array to output a new array matching the functions output criteria.
As an aside, please read up on how to provide a minimal example if you have an array of objects it helps to have an actual array of objects and not a string that needs to be converted in order to create a working example.
Edit
Added a second reducer to total the gjendje key on matched Kodartikulli keys, you could do this with one reducer, but (personal opinion) I think it is generally a better practice to keep your functions narrow in scope.
const data = [
{
BARKOD:"Pa Detatime",
DETAJIM1:"",
DETAJIM2:"",
DTMODIFIKIM:"2017-10-02T16:06:53.206Z",
KODARTIKULLI:"SD13137",
KODI:"MX02",
KODNJESIA1:"cope",
PERSHKRIMARTIKULLI:"Emporio Armani 4097 5574 71 56",
cmimibaze:0,
gjendje:1,
},
{
BARKOD:"Pa Detatime",
DETAJIM1:"",
DETAJIM2:"",
DTMODIFIKIM:"2017-10-02T16:06:53.206Z",
KODARTIKULLI:"SD13137",
KODI:"MX02",
KODNJESIA1:"cope",
PERSHKRIMARTIKULLI:"Emporio Armani 4097 5574 71 56",
cmimibaze:0,
gjendje:1,
},
{
BARKOD:"Pa Detatime",
DETAJIM1:"",
DETAJIM2:"",
DTMODIFIKIM:"2017-10-02T16:06:53.206Z",
KODARTIKULLI:"SD13137",
KODI:"MX03",
KODNJESIA1:"cope",
PERSHKRIMARTIKULLI:"Emporio Armani 4097 5574 71 56",
cmimibaze:0,
gjendje:1,
},
{
BARKOD:"Pa Detatime",
DETAJIM1:"",
DETAJIM2:"",
DTMODIFIKIM:"2017-10-02T16:06:53.206Z",
KODARTIKULLI:"SD13141",
KODI:"MX02",
KODNJESIA1:"cope",
PERSHKRIMARTIKULLI:"Emporio Armani 4097 5574 71 56",
cmimibaze:0,
gjendje:1,
}
];
constdedupe = (group, current) => {
const index = group.findIndex(val => (val.KODI == current.KODI && val.KODARTIKULLI == current.KODARTIKULLI));
if (index === -1) {
group.push(current);
}
return group;
};
consttotals = (group, current) => {
const index = group.findIndex(val => val.KODARTIKULLI == current.KODARTIKULLI);
if (index === -1) {
return [ ...group, current];
}
group[index].gjendje += current.gjendje;
return group;
};
const result = data.reduce(dedupe, []).reduce(totals, []);
console.log(result);
Post a Comment for "Loop Through Array Of Objects And Add Them Based On A Property"