首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何根据对象数组的属性和来组织对象数组?

如何根据对象数组的属性和来组织对象数组?
EN

Stack Overflow用户
提问于 2022-05-20 01:33:28
回答 4查看 158关注 0票数 3

我正在为一个销售应用程序开发backEnd部分,其中我有两个Objects数组,一个得到卖家的名字,另一个得到每月的销售额。我的上级告诉我,使用包含销售额的数组中的数据,将人名数组从销售额最多的数组排序到最小(从最高到最低)的数组,基本上是对关键的=>数量进行求和。我不知道怎么做,我试着用一种减少方法来处理每个销售商的销售,但是我想不出如何比较它们来重组Array。守则是:

代码语言:javascript
复制
const sellers= [{id:1,name: 'juan', age: 23},{id:2,name: 'adrian', age: 32},{id:3,name: 'apolo', age: 45}];

const sales= [{equipo: 'frances', sellerId: 2, quantity: 234},{equipo: 'italiano', sellerId: 3, quantity: 24},{equipo: 'polaco', sellerId: 1, quantity: 534},{equipo: 'frances', sellerId: 2, quantity: 1234},{equipo: 'frances', sellerId: 3, quantity: 2342}];

这就是我已经尝试过的:

代码语言:javascript
复制
const bestSellers= () => {
  sales.reduce((sum, value) => ( value.sellerId == 1 ? sum + value.area : sum), 0); }

最终结果应该如下所示:

代码语言:javascript
复制
const sellers= [{id:3,name: 'apolo', age: 45},{id:2,name: 'adrian', age: 32},{id:1,name: 'juan', age: 23}]
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2022-05-20 02:02:32

你在这里想做两件事。

  1. 找出每个卖家的总销售额。
  2. 对每个销售商的总销售额进行排序。

在我的排序函数中,您可以看到,我正在过滤卖方的所有销售。

一旦我有一个销售者的销售,我使用减少方法,把他们的销售数量之和为一个容易使用的数字。

然后,我比较以前的卖家数量和当前的卖家数量,用排序方法重新订购。

我鼓励您阅读所使用方法的文档,以便您了解每一步所发生的事情。

使用的方法:排序 过滤器 Reduce

代码语言:javascript
复制
const sellers = [{
  id: 1,
  name: 'juan',
  age: 23
}, {
  id: 2,
  name: 'adrian',
  age: 32
}, {
  id: 3,
  name: 'apolo',
  age: 45
}];

const sales = [{
  equipo: 'frances',
  sellerId: 2,
  quantity: 234
}, {
  equipo: 'italiano',
  sellerId: 3,
  quantity: 24
}, {
  equipo: 'polaco',
  sellerId: 1,
  quantity: 534
}, {
  equipo: 'frances',
  sellerId: 2,
  quantity: 1234
}, {
  equipo: 'frances',
  sellerId: 3,
  quantity: 2342
}];

const expected = [{
  id: 3,
  name: 'apolo',
  age: 45
}, {
  id: 2,
  name: 'adrian',
  age: 32
}, {
  id: 1,
  name: 'juan',
  age: 23
}]

const result = sellers.sort((a, b) => {
  totalA = sales.filter(sale => sale.sellerId === a.id).reduce((acc, val) => acc + val.quantity, 0)
  totalB = sales.filter(sale => sale.sellerId === b.id).reduce((acc, val) => acc + val.quantity, 0)
  return totalB - totalA
})

// Check we get what we expect
console.log(JSON.stringify(expected) === JSON.stringify(result))

票数 1
EN

Stack Overflow用户

发布于 2022-05-20 02:09:01

首先,将sales数组缩小为每个卖方的数量之和,对此列表进行排序,然后从sellers数组映射到相应的卖方。

代码语言:javascript
复制
const sellers= [{id:1,name: 'juan', age: 23},{id:2,name: 'adrian', age: 32},{id:3,name: 'apolo', age: 45}];

const sales= [{equipo: 'frances', sellerId: 2, quantity: 234},{equipo: 'italiano', sellerId: 3, quantity: 24},{equipo: 'polaco', sellerId: 1, quantity: 534},{equipo: 'frances', sellerId: 2, quantity: 1234},{equipo: 'frances', sellerId: 3, quantity: 2342}];

const bestSellers = (sellers, sales) => {
  let res = sales.reduce((result, sale) => {
    // Check if seller is already added in result
    const obj = result.find(s => s.sellerId === sale.sellerId);
    if (obj) {
      // If seller already added then increase it's quantity
        obj.quantity += sale.quantity;
    } else {
      // If seller not added, add seller in result
        result.push({ sellerId: sale.sellerId, quantity: sale.quantity });
    }
    return result;
  }, []);
    
  // Sort result by quantity in decreasing order 
  res.sort((a,b) => b.quantity - a.quantity);
  
  // Map each sale to the seller
  res = res.map(s => sellers.find(seller => seller.id === s.sellerId));
    
  return res;
}

const result = bestSellers(sellers, sales);
console.log(result);

票数 1
EN

Stack Overflow用户

发布于 2022-05-20 02:14:33

你可以这样做:

代码语言:javascript
复制
const
  sellers = 
    [ { id: 1, name: 'juan',   age: 23 } 
    , { id: 2, name: 'adrian', age: 32 } 
    , { id: 3, name: 'apolo',  age: 45 } 
    ] 
, sales = 
    [ { equipo: 'frances',  sellerId: 2, quantity:  234 } 
    , { equipo: 'italiano', sellerId: 3, quantity:   24 } 
    , { equipo: 'polaco',   sellerId: 1, quantity:  534 } 
    , { equipo: 'frances',  sellerId: 2, quantity: 1234 } 
    , { equipo: 'frances',  sellerId: 3, quantity: 2342 } 
    ];


sellers.forEach( ({id},i,all) => // add a sum attribute 
  {
  all[i].sum = sales
                .filter(({sellerId})=>sellerId===id)
                .reduce((sum,{quantity})=>sum+quantity,0)
  });

sellers
  .sort( (a,b) => b.sum - a.sum)                 
  .forEach((seller,i,all) => delete seller.sum ) // remove the sum

console.log( sellers )
代码语言:javascript
复制
.as-console-wrapper {max-height: 100% !important;top: 0;}
.as-console-row::after {display: none !important;}

或者:

代码语言:javascript
复制
const
  sellers = 
    [ { id: 1, name: 'juan',   age: 23 } 
    , { id: 2, name: 'adrian', age: 32 } 
    , { id: 3, name: 'apolo',  age: 45 } 
    ] 
, sales = 
    [ { equipo: 'frances',  sellerId: 2, quantity:  234 } 
    , { equipo: 'italiano', sellerId: 3, quantity:   24 } 
    , { equipo: 'polaco',   sellerId: 1, quantity:  534 } 
    , { equipo: 'frances',  sellerId: 2, quantity: 1234 } 
    , { equipo: 'frances',  sellerId: 3, quantity: 2342 } 
    ] 

// compute all the sum in a new Sums object:
const Sums = sales.reduce((sum,{sellerId : id, quantity}) =>
  ( sum[id] ??= 0, sum[id] += quantity , sum ), {} )

sellers .sort( (a,b) => Sums[b.id] - Sums[a.id])

console.log( sellers )
代码语言:javascript
复制
.as-console-wrapper {max-height: 100% !important;top: 0;}
.as-console-row::after {display: none !important;}

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72312675

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档