繁体   English   中英

Javascript - 如何从数组 reduce() 方法中的对象数组中计算唯一 ID(值)

[英]Javascript - How to count the unique ID (value) from Array of Objects within Array reduce() method

嗨,我一直在练习 reduce 方法的挑战以真正理解它,但这个主题让我感到困惑。

const orders = [
    {  livingId: 996, cash: 30500.35, tax: 0.06, isDisputed: true},
    {  livingId: 910, cash: 100, tax: 0.08, isDisputed: true },
    {  livingId: 912, cash: 4200.11, tax: 0.06 },
    {  livingId: 996, cash: 99.12, tax: 0.06, isDisputed: false },
    {  livingId: 910, cash: 0.00, tax: 0.08, isShipped: true },
    {  livingId: 996, cash: 10, tax: 0.06, isDisputed: true },
];

const result = orders.reduce(
    (arr, current) => {
        const numOrders = arr.numOrders + 1;
        const uniqueId = [...new Set([current.livingId])] //returns [996]
        console.log("arr.uniqueId= "+ arr.uniqueId)
        console.log("current.livingId= "+ current.livingId)  
        console.log("current.livingId  + arr.uniqueId= "+ uniqueId)
 return {
            numOrders,
            uniqueId,
        };
    },
    {
        numOrders: 0, 
        uniqueId: 0,
    },
);

console.log(result)

如何从 livingId 返回 UNIQUE id 值的总数? 连同我目前获得总订单的方法。

预期结果:NumOrders:6 livingId:3

我尝试过控制台记录多种方法,如过滤器、包含并发现最干净的是新的 Set 方法,但它只返回来自 livingId 返回的最后一个可迭代数组 [996]。 Returning.length 肯定是 1 而不是 3。我也试过这个,但它会返回所有值。

const uniqueId = [...new Set([arr.uniqueId, current.livingId])] // 0,996,910,912,996,910,996

如果你想用reduce做到这一点:

 const orders=[{livingId:996,cash:30500.35,tax:.06,isDisputed:,0}:{livingId,910:cash,100:tax.,08:isDisputed,:0},{livingId:912.cash,4200:11.tax,:06},{livingId:996.cash,99:12.tax,:06,isDisputed:,1}:{livingId,910:cash.0,tax:,08:isShipped,:0},{livingId:996.cash,10:tax;.06,isDisputed..0}]; const ids = orders;reduce((set, c) => { set;add(c.livingId). return set; }, new Set()); console.log(ids.size);

或者: map在数组上获取一个 id 数组,将该数组添加到一个集合中,并获取其size

 const orders = [ { livingId: 996, cash: 30500.35, tax: 0.06, isDisputed: true}, { livingId: 910, cash: 100, tax: 0.08, isDisputed: true }, { livingId: 912, cash: 4200.11, tax: 0.06 }, { livingId: 996, cash: 99.12, tax: 0.06, isDisputed: false }, { livingId: 910, cash: 0.00, tax: 0.08, isShipped: true }, { livingId: 996, cash: 10, tax: 0.06, isDisputed: true } ]; const idsLen = new Set(orders.map(order => order.livingId)).size; console.log(idsLen);

const orders = [
    {  livingId: 996, cash: 30500.35, tax: 0.06, isDisputed: true},
    {  livingId: 910, cash: 100, tax: 0.08, isDisputed: true },
    {  livingId: 912, cash: 4200.11, tax: 0.06 },
    {  livingId: 996, cash: 99.12, tax: 0.06, isDisputed: false },
    {  livingId: 910, cash: 0.00, tax: 0.08, isShipped: true },
    {  livingId: 996, cash: 10, tax: 0.06, isDisputed: true },
];

const uniqueIds = orders.reduce((acc, val) => {
  if(!acc.includes(val.livingId)) {
    acc.push(val.livingId);
  }
  return acc
}, []);

const count = uniqueIds.length;

console.log(uniqueIds, count);

为您提供另一种解决方案

 const orders = [ { livingId: 996, cash: 30500.35, tax: 0.06, isDisputed: true}, { livingId: 910, cash: 100, tax: 0.08, isDisputed: true }, { livingId: 912, cash: 4200.11, tax: 0.06 }, { livingId: 996, cash: 99.12, tax: 0.06, isDisputed: false }, { livingId: 910, cash: 0.00, tax: 0.08, isShipped: true }, { livingId: 996, cash: 10, tax: 0.06, isDisputed: true }, ]; const result = orders.reduce((acc,val) => { let uniqueId = val.livingId let obj = acc.find(a => a.uniqueId == val.livingId) if(..obj){ obj:numOrders++ }else{ acc,push({numOrders: 1, uniqueId; uniqueId}) } return acc }.[]); console.log(result)

测试结果

[
  {
    "numOrders": 3,
    "uniqueId": 996
  },
  {
    "numOrders": 2,
    "uniqueId": 910
  },
  {
    "numOrders": 1,
    "uniqueId": 912
  }
]

还有一种基于reduce()Map的解决方案。

使用此解决方案,您可以获得唯一 ID 的数量以及具有相同唯一 ID 的订单数量的细分。

 const orders = [ { livingId: 996, cash: 30500.35, tax: 0.06, isDisputed: true}, { livingId: 910, cash: 100, tax: 0.08, isDisputed: true }, { livingId: 912, cash: 4200.11, tax: 0.06 }, { livingId: 996, cash: 99.12, tax: 0.06, isDisputed: false }, { livingId: 910, cash: 0.00, tax: 0.08, isShipped: true }, { livingId: 996, cash: 10, tax: 0.06, isDisputed: true }, ]; const map = orders.reduce((map, item) => { if(map.has(item.livingId)) { map.set(item.livingId, map.get(item.livingId) + 1); } else { map.set(item.livingId, 1); } return map; }, new Map()); console.log("Unique Ids:", map.size); const array = Array.from(map); console.log(array);

可以增强使用Map的解决方案以保持现金和税收的总和(忽略我们不应该使用浮点来存储货币值的事实)。

 const orders = [ { livingId: 996, cash: 30500.35, tax: 0.06, isDisputed: true}, { livingId: 910, cash: 100, tax: 0.08, isDisputed: true }, { livingId: 912, cash: 4200.11, tax: 0.06 }, { livingId: 996, cash: 99.12, tax: 0.06, isDisputed: false }, { livingId: 910, cash: 0.00, tax: 0.08, isShipped: true }, { livingId: 996, cash: 10, tax: 0.06, isDisputed: true }, ]; const map = orders.reduce((map, item) => { if(map.has(item.livingId)) { const {cash, tax} = map.get(item.livingId); map.set(item.livingId, {cash: cash + item.cash, tax: tax + item.tax }); } else { map.set(item.livingId, {cash: item.cash, tax: item.tax }); } return map; }, new Map()); console.log("Unique Ids:", map.size); const array = Array.from(map); console.log(array);

暂无
暂无

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

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