JavaScript objects array filter by minimum value of an attribute

隐身守侯 提交于 2021-01-27 13:46:51

问题


I need to filter this object array by minimum value of 'rest' attribute. This is an one way to do it. Is there any other ways ?

'data' variable is a result of chained function. Is there any other way to do this without calling 'data' variable again inside Math.min() function.

let data = 
[ { size: 5, qty: 2, rest: 0 },
  { size: 2, qty: 5, rest: 0 },
  { size: 1, qty: 10, rest: 0 },
  { size: 3, qty: 3, rest: 1 },
  { size: 4, qty: 2, rest: 2 } ]

let result = data.filter(e=> e.rest === Math.min(...data.map(f=>f.rest) ) );
console.log(result);

// result is
//[ { size: 5, qty: 2, rest: 0 },
//  { size: 2, qty: 5, rest: 0 },
//  { size: 1, qty: 10, rest: 0 }]

回答1:


imo. the simplest/best solution is the one @CertainPerformance gave you.

Just wanted to add another solution with linear runtime (that truly iterates only once over the Array)

let data = [
  { size: 5, qty: 2, rest: 0 },
  { size: 2, qty: 5, rest: 0 },
  { size: 1, qty: 10, rest: 0 },
  { size: 3, qty: 3, rest: 1 },
  { size: 4, qty: 2, rest: 2 } 
];

let result = data.reduce((result, item) => {
  let minRest = result.length? result[0].rest: item.rest;

  if (item.rest < minRest) {
    minRest = item.rest;
    result.length = 0;
  }

  if (item.rest === minRest) {
    result.push(item);
  }

  return result;
}, []);

console.log(result);

@mathieux51 got me another idea how you can do this inside a method chain, but the readability/clarity/intention is not as good as with the other approaches:

let data = [
  { size: 5, qty: 2, rest: 0 },
  { size: 2, qty: 5, rest: 0 },
  { size: 1, qty: 10, rest: 0 },
  { size: 3, qty: 3, rest: 1 },
  { size: 4, qty: 2, rest: 2 } 
];

let result = data.sort((a, b) => a.rest - b.rest)
                 .filter((item, index, array) => item.rest === array[0].rest);

console.log(result);



回答2:


The easiest way is to pull the min function out of the filter like this:

let min = Math.min(...data.map(item => item.rest))

This is much more efficient as we are no longer loop over the data to find the min for every iteration of the filter.

We now have n * 2 passes instead of n^2 passes. (n is the size of your data set, 5 in this case)

Full example below:

   let data = [ 
     { size: 5, qty: 2, rest: 0 },
     { size: 2, qty: 5, rest: 0 },
     { size: 1, qty: 10, rest: 0 },
     { size: 3, qty: 3, rest: 1 },
     { size: 4, qty: 2, rest: 2 } 
   ]

  let min = Math.min(...data.map(item => item.rest))
  let result = data.filter(item => item.rest === min)
  console.log(result)

Hope this helps!

Lloyd




回答3:


data.map inside of data.filter is O(N^2); for an O(N) solution, iterate through data ahead of time to calculate the minimum, then filter by that minimum:

let data = 
[ { size: 5, qty: 2, rest: 0 },
  { size: 2, qty: 5, rest: 0 },
  { size: 1, qty: 10, rest: 0 },
  { size: 3, qty: 3, rest: 1 },
  { size: 4, qty: 2, rest: 2 } ];
const minRest = Math.min(...data.map(({ rest }) => rest));

let result = data.filter(({ rest }) => rest === minRest);
console.log(result);



回答4:


It sounds like you want to sort the list. I would do it as following:

const result = data.sort((a, b) => a.rest - b.rest)


来源:https://stackoverflow.com/questions/53097817/javascript-objects-array-filter-by-minimum-value-of-an-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!