简体   繁体   English

使用 lodash(或 vanilla javascript)对对象数组进行排序和过滤

[英]sort and filter an object array with lodash (or vanilla javascript)

I have this object I want to sort and filter by retaining only the 2 highest values by object.我有这个对象,我想通过只保留对象的 2 个最高值来排序和过滤。

obj={ A :[{
      asset: 9,
      biodiversity: 4,
      infrastructure: 15,
      deflation: 11,
      energy: 9
    }],
  B:[{
      asset: 12,
      biodiversity: 10,
      infrastructure: 9,
      deflation: 7,
      energy: 15
    }],
  C:[{
      asset: 2,
      biodiversity: 12,
      infrastructure: 6,
      deflation: 6,
      energy: 8
    }]}

I would like to sort the objects by their values and filter out the 2 highest eg:我想按对象的值对对象进行排序并过滤掉 2 个最高值,例如:

{A :[{
      infrastructure: 15,
      deflation: 11
    }],
  B:[{
      energy: 15,
      asset: 12
    }],
  C:[{
      biodiversity: 12,
      energy: 8
    }]}

I have tried this for sorting:我试过这个排序:

Object.keys(obj).forEach((a) => _.sortBy(obj[a][0])))

But that is wrong obviously.但这显然是错误的。 I am using lodash but will accept vanilla javascript solution as well.我正在使用 lodash,但也会接受 vanilla javascript 解决方案。

You could get the entries of the inner objects and sort by value descending, get the top two key/value pairs and build a new object from it.您可以获取内部对象的条目并按值降序排序,获取前两个键/值对并从中构建一个新对象。

 const data = { A: [{ asset: 9, biodiversity: 4, infrastructure: 15, deflation: 11, energy: 9 }], B: [{ asset: 12, biodiversity: 10, infrastructure: 9, deflation: 7, nergy: 15 }], C: [{ asset: 2, biodiversity: 12, infrastructure: 6, deflation: 6, energy: 8 }]}, result = Object.fromEntries(Object .entries(data) .map(([k, a]) => [k, a.map(o => Object.fromEntries(Object .entries(o) .sort((a, b) => b[1] - a[1]) .slice(0, 2) ))]) ); console.log(result);
 .as-console-wrapper { max-height: 100% !important; top: 0; }

(Edit/Note: This is based on the code you originally posted. I'm glad to see you updated your question and got rid of the wrapping array.) (编辑/注意:这是基于您最初发布的代码。我很高兴看到您更新了您的问题并摆脱了包装数组。)

Here's a relatively functional approach.这是一个相对实用的方法。

The secondLargestValue function finds the threshold value within each object. secondLargestValue函数在每个对象中查找阈值。 The copyWithoutValsBelowThreshold function gives us a modified copy of the object. copyWithoutValsBelowThreshold函数为我们提供了对象的修改副本。

We loop though the entries in the top-level object and apply these two functions.我们循环遍历顶级对象中的条目并应用这两个函数。 See comments in the code for further clarification.有关进一步说明,请参阅代码中的注释。

 let json = getArray(); // Identifies the original array // Defines `secondLargestVal` function const secondLargestVal = someObj => // Gets second item of array after sorting numerically in descending order Object.values(someObj).sort((a, b) => b - a)[1]; // Defines `copyWithoutValsBelowThreshold` function const copyWithoutValsBelowThreshold = ( (someObj, threshold) => { // This function doesn't mutate the original value clone = Object.assign({}, someObj); // Copies obj for(let prop in clone){ // Loops through properties, deleting non-qualifying ones if(clone[prop] < threshold){ delete clone[prop]; } } return clone; }); // Defines our main function const mutateJson = () => { let entries = Object.entries(json[0]); entries = entries.map(entry => { // `entry[0]` is the property name (eg: 'A') -- we don't actually use this // `entry[1]` is the value (in this case, an array containing a single object) let obj = entry[1][0]; // Identifies the actual object const threshold = secondLargestVal(obj); // Identifies minimum value // Updates the entry, deleting properties whose values are too low entry[1][0] = copyWithoutValsBelowThreshold(obj, threshold); return entry; }); json[0] = Object.fromEntries(entries); // Replaces the top-level object } // Calls the main function mutateJson(); console.log(json); // Provides the original array function getArray(){ return [{ A :[{ asset: 9, biodiversity: 4, infrastructure: 15, deflation: 11, energy: 9 }], B:[{ asset: 12, biodiversity: 10, infrastructure: 9, deflation: 7, energy: 15 }], C:[{ asset: 2, biodiversity: 12, infrastructure: 6, deflation: 6, energy: 8 }]}] }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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