Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
506 views
in Technique[技术] by (31.9m points)

node.js - How to perform addition of same keys' values of objects and return unique objects from array of objects using javascript?

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"
  }
]

Code I've tried in Codepen

Is there anything am making mistakes in getting the result?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (31.9m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...