am working on a task where I get a response consists of array of objects.
Now, I want to perform addition on some fields based on particular conditions and return new and unique array of objects without any duplicates.
Here is the response.
let respData = [
{
"NEW": 0,
"Working": 0,
"Not Working": 2,
"Broken": 0,
"Location": "Karimnagar",
"TotalPrice": 274500,
"AssetsCount": 2,
"AssetType": "Hardware Devices"
},{
"NEW": 1,
"Working": 0,
"Not Working": 0,
"Broken": 0,
"Location": "chennai",
"TotalPrice": 4500,
"AssetsCount": 1,
"AssetType": "Undefined"
},{
"NEW": 0,
"Working": 0,
"Not Working": 0,
"Broken": 0,
"Location": "Undefined",
"TotalPrice": 0,
"AssetsCount": 1,
"AssetType": "Undefined"
},{
"NEW": 1,
"Working": 0,
"Not Working": 0,
"Broken": 0,
"Location": "Karimnagar",
"TotalPrice": 12500,
"AssetsCount": 1,
"AssetType": "Hardware Devices"
},{
"NEW": 0,
"Working": 1,
"Not Working": 0,
"Broken": 0,
"Location": "Karimnagar",
"TotalPrice": 1200,
"AssetsCount": 1,
"AssetType": "Hardware Devices"
}
]
Now am trying to add the integer values in objects if AssetType
and Location
are same.
If there are not matching results present, need to return them as it is.
And after adding them, I need to return unique array of objects with all updated values.
I've tried with map
and filter
to get the result, but couldn't get the correct result.
This is the code I've tried.
let respCopy = respData;
let finalObj = {}, testArray = [];
respCopy.forEach((copyData, i) => {
respData.forEach((data, j) => {
if(i !== j && copyData.AssetType === data.AssetType && copyData.Location === data.Location){
assetConditions.forEach((condition, k) => {
if(copyData[condition.name]){
finalObj[condition.name] = copyData[condition.name] + data[condition.name];
finalObj["AssetType"] = copyData.AssetType;
finalObj["Location"] = copyData.Location;
copyData[condition.name] = 0;
data[condition.name] = 0;
}
})
}else if(i == j && copyData.AssetType === data.AssetType && copyData.Location === data.Location){
assetConditions.forEach((condition, k) => {
if(copyData[condition.name]){
finalObj[condition.name] = copyData[condition.name];
finalObj["AssetType"] = copyData.AssetType;
finalObj["Location"] = copyData.Location;
copyData[condition.name] = 0;
data[condition.name] = 0;
}
})
}
});
testArray.push(finalObj);
})
This is the expected result.
[
{
"NEW": 1,
"Working": 1,
"Not Working": 2,
"Broken": 0,
"Location": "Karimnagar",
"TotalPrice": 288200,
"AssetsCount": 4,
"AssetType": "Hardware Devices"
},{
"NEW": 1,
"Working": 0,
"Not Working": 0,
"Broken": 0,
"Location": "chennai",
"TotalPrice": 4500,
"AssetsCount": 1,
"AssetType": "Undefined"
},{
"NEW": 0,
"Working": 0,
"Not Working": 0,
"Broken": 0,
"Location": "Undefined",
"TotalPrice": 0,
"AssetsCount": 1,
"AssetType": "Undefined"
}
]
Is there anything am making mistakes in getting the result?
This type of issues can be easily solved using a dictionary object as a proxy.
You have to define the object's keys as your unique identifier (in your case a concatenation of AssetType
and Location
values) then map them to the objects you want to create a unique array of.
Eventually you'll want to return the Object.values()
of your dictionary object:
const arrayWithDuplicates = [{ key1: 1, key2: 2, foo: 1 }, { key1: 1, key2: 2, foo: 2 }, // Note the two first elements have identical keys { key1: 3, key2: 2, foo: 3 }, ]; function getDictKey(element) { // This can obviously be implemented otherwise const { key1, key2 } = element; return `${key1}_${key2}`; } const dictionary = {}; arrayWithDuplicates.forEach((element) => { const key = getDictKey(element); if (dictionary[key]) { dictionary[key].foo += element.foo; } else { dictionary[key] = element; } }); const arrayWithoutDuplicates = Object.values(dictionary); console.log(arrayWithoutDuplicates);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.