查找值为Price并按类别分组的最小对象(JavaScript)



我有一个像这样的对象数组:

const fruits =
[
{ id: 1, fruit: "apple", store: "store1", price: 1 }
, { id: 2, fruit: "apple", store: "store2", price: 1.25 }
, { id: 3, fruit: "banana", store: "store1", price: 0.5 }
, { id: 4, fruit: "banana", store: "store2", price: 0.75 }
, { id: 5, fruit: "Orange", store: "store3", price: 5 }
, { id: 6, fruit: "Orange", store: "store1", price: 5.1 }
, { id: 7, fruit: "Cherry", store: "store1", price: .1 }
, { id: 8, fruit: "Cherry", store: "store1", price: .6 }
, { id: 9, fruit: "", store: "store1", price: 0.5 }
]

现在,我想按照升序将它们按类别分组,我可以使用以下方法:

const groupBy = (fruits.sort((a, b) => a.price - b.price)).reduce((grouped, object) => {
let x = object["fruit"]
if (!grouped[x]) {
grouped[x] = []
}

grouped[x].push(object)
return grouped
}, [])

上述代码的输出如预期:-

"": [{…}]
Cherry: (2) [{…}, {…}]
Orange: (2) [{…}, {…}]
apple: (2) [{…}, {…}]
banana: (2) [{…}, {…}]

现在(问题),我只想要该类别具有最低价格的对象。我尝试了下面的代码,但输出与上面的代码相同:

const groupBy = (fruits.sort((a, b) => a.price - b.price)).reduce((grouped, object) => {
let x = object["fruit"]
if (!grouped[x]) {
grouped[x] = []
}
else if (grouped[x]?.price < object?.price) {
let c = grouped[x]
return grouped[x]
}
grouped[x].push(object)
return grouped
}, [])

因为您已经根据不同的商品价格进行排序,所以您可以简单地只推入数组if (!grouped[x])。请看这里:

const fruits = [
{ id: 1, fruit: 'apple', store: 'store1', price: 1 },
{ id: 2, fruit: 'apple', store: 'store2', price: 1.25 },
{ id: 3, fruit: 'banana', store: 'store1', price: 0.5 },
{ id: 4, fruit: 'banana', store: 'store2', price: 0.75 },
{ id: 5, fruit: 'Orange', store: 'store3', price: 5 },
{ id: 6, fruit: 'Orange', store: 'store1', price: 5.1 },
{ id: 7, fruit: 'Cherry', store: 'store1', price: 0.1 },
{ id: 8, fruit: 'Cherry', store: 'store1', price: 0.6 },
{ id: 9, fruit: '', store: 'store1', price: 0.5 },
];
const groupBy = fruits
.sort((a, b) => a.price - b.price)
.reduce((grouped, object) => {
let x = object['fruit'];
if (!grouped[x]) {
grouped[x] = [];
grouped[x].push(object);
}
return grouped;
}, []);
console.log(groupBy);

——UPDATE——我认为另一个比reduce函数更简洁的解决方案是:

  1. 根据每个物品的价格对fruits数组进行排序
  2. 创建结果空对象并循环通过已排序的水果
  3. 如果结果对象没有每个水果名称的键:我们将水果对象推入结果对象

代码如下:

const result = {};
fruits
.sort((a, b) => a.price - b.price)
.forEach((item) => {
if (!(item.fruit in result)) {
result[item.fruit] = item;
}
});
console.log(result);

不确定为什么要创建一个数组-看起来您想要的是一个对象。要找到最低价格,您可以使用Math.min。注意,我使用了一些简写来进行比较:

...
grouped[x] = Math.min(grouped[x] ?? 10000, object.price)
...

意思是"如果没有当前价格,使用10000">

const fruits = [
{ id: 1, fruit: 'apple', store: 'store1', price: 1 },
{ id: 2, fruit: 'apple', store: 'store2', price: 1.25 },
{ id: 3, fruit: 'banana', store: 'store1', price: 0.5 },
{ id: 4, fruit: 'banana', store: 'store2', price: 0.75 },
{ id: 5, fruit: 'Orange', store: 'store3', price: 5 },
{ id: 6, fruit: 'Orange', store: 'store1', price: 5.1 },
{ id: 7, fruit: 'Cherry', store: 'store1', price: 0.1 },
{ id: 8, fruit: 'Cherry', store: 'store1', price: 0.6 },
{ id: 9, fruit: '', store: 'store1', price: 0.5 },
];
const groupBy = fruits.reduce((grouped, object) => {
let x = object["fruit"];
if(x.trim()=="") return grouped;
grouped[x] = Math.min(grouped[x] ?? Infinity, object.price)
return grouped;
}, {})
console.log(groupBy)

最新更新