简体   繁体   中英

Filter out array with least sum from a list of Objects in javascript

I have an object that looks like this:

数据集截图

I want to add the entries for each of the arrays and from that I want to filter out 3 arrays which has the least sum and I want to reject the 3 objects with lowest sum values.

I am trying the below code:

 const dataSets = { "051670D9: eth0": [ 338055, 338468, 338872 ], "051670D9: eth1": [ 0, 0, 0 ], "051670D9: eth2": [ 0, 0, 0 ], "094FAF52: eth0": [ 316041, 316663, 317050 ], "094FAF52: eth1": [ 0, 0, 0 ], "094FAF52: eth2": [ 0, 0, 0 ], "0CC78763: eth0": [ 399949, 00432, 400861 ], "0CC78763: eth1": [ 0, 0, 0 ], "0CC78763: eth2": [ 0, 0, 0 ] } const countObj = {} Object.keys(dataSets).forEach(key => { const sum = dataSets[key].reduce((acc, item) => acc+=item); countObj[key] = sum; }); console.log(countObj);

the easier way is to convert object into sortable array and sort it by the sum of the elements and then get the elements with lowest sum values.

const dataSets = {
    "051670D9 : eth0": [  338055, 338468,   338872  ],
    "051670D9 : eth1": [   0, 0,   0  ],
    "051670D9 : eth2": [ 0,  0,   0 ],
    "094FAF52 : eth0": [ 316041, 316663, 317050 ],
    "094FAF52 : eth1": [ 0, 0,  0 ],
    "094FAF52 : eth2": [ 0, 0, 0  ],
    "0CC78763 : eth0": [  399949, 00432, 400861  ],
    "0CC78763 : eth1": [  0,  0,  0  ],
    "0CC78763 : eth2": [ 0, 0, 0  ]
  }

  var sortable = [];

  for (var key in dataSets) {
    sortable.push([key, dataSets[key]]);
  }

  function sortFunction(a,b) {
    return a[1].reduce((acc, item) => acc+=item) - b[1].reduce((acc, item) => acc+=item);
  }

  sortable.sort(sortFunction);


  var filtered= sortable.slice(3);

  console.log(filtered);

this filtered array contains the keys and arrays you want.

By using an array to store objects that contain the object key and sum value, you can sort the results. Then use the stored keys to modify the original object. I recommend cloning the original object if you might need it at another point. Otherwise, you can skip the cloning step, as it might eat up some RAM if you have large datasets.

Please see the comments in the code below

 const dataSets = { "051670D9: eth0": [ 338055, 338468, 338872 ], "051670D9: eth1": [ 0, 0, 0 ], "051670D9: eth2": [ 0, 0, 0 ], "094FAF52: eth0": [ 316041, 316663, 317050 ], "094FAF52: eth1": [ 0, 0, 0 ], "094FAF52: eth2": [ 0, 0, 0 ], "0CC78763: eth0": [ 399949, 00432, 400861 ], "0CC78763: eth1": [ 0, 0, 0 ], "0CC78763: eth2": [ 0, 0, 0 ] } function filterOutLowestThree(dataSetsInput) { // clone so we don't modify the original object let input = JSON.parse(JSON.stringify(dataSetsInput)); // Using an array, as they are sortable let countObj = []; Object.keys(input).forEach(key => { // Putting the sum in the array as an object, storing the array key and the sum value. let sum = dataSets[key].reduce((acc, item) => acc+=item); countObj.push({key: key, sum: sum}); }); // Sort the array by sum values. countObj.sort((a,b) => b.sum - a.sum); // Get the last 3 entries from the array. let deletionCandidates = countObj.splice(-3); // Delete the keys from the clones input object. deletionCandidates.forEach((entry) => { delete input[entry.key]; }) return input; } console.log("Original dataset", dataSets); console.log("Filtered dataset", filterOutLowestThree(dataSets));

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM