【问题标题】:How to compare key with sum of array of objects' keys?如何将键与对象键数组的总和进行比较?
【发布时间】:2020-10-17 03:49:08
【问题描述】:

我有对象数组ob1ob2,我想检查每个对象的总量total 是否等于info 中对象的qtys 的总和数组。

如果所有对象的total = sum(info.qty) 则返回一个空数组,否则返回具有无效total 字段的对象。

到目前为止我的尝试:

function checktotal(ob1) {
    var result = ob1.forEach(e => {
     if (e.info.length > 0) {
        var sum = e.info.reduce((a, {qty}) => a + qty, 0);
       if (sum !== e.total) {
        return "total qty not matches with info total qty,"+e
       }
   })     
}

var ob1 = [
 {id:1, total: 2, info:[{idx:1, qty:1},{idx: 2, qty: 2}] },
 {id:2, total:1, info: [{idx:1, qty:1}] };
];
var ob2 = [
 {id:1, total: 1, info:[{idx:1, qty:1}] },
 {id:2, total:4, info: [{idx:1, qty:2},{idx: 2, qty: 2}] };
];

预期输出:

checktotal(ob1); // should return [{id:1, total:2, info:[{idx:1, qty:1}, {idx:2, qty:2}]}]
checktotal(ob2); // should return []

【问题讨论】:

  • .forEach() 不返回任何内容。
  • 如果多个条目与total 值不匹配会怎样? ob1[1].total === 2 会是什么结果?
  • 您的代码中有几个语法错误,可能来自复制和粘贴。例如,if(e.info.length > 0) { 块缺少结束括号 }。如果您使用Stack Snippets 创建了minimal reproducible example(图标在编辑器工具栏中的页面中类似于<>),使用console.log 显示结果,然后单击运行以确保没有错误,这将很有帮助。

标签: javascript arrays object nested-object


【解决方案1】:

我认为您正在寻找一个函数,该函数从输入对象数组中返回对象子集,其中 total 中的总量不等于 @987654325 内部对象中 qty 中的总量之和@。

您可以使用filter() 来获取不匹配对象的数组,并且检查总和的条件使用reduce()。下面是一个工作示例。

请注意,与您要求的不同,对第一个对象数组的调用返回一个包含罪魁祸首对象的数组,而不仅仅是对象本身。

function checkTotal(data) {
    return data.filter(({ total, info }) => (
        total !== info.reduce((acc, { qty }) => (acc + qty), 0)
    ));
}

var ob1 = [
    { id: 1, total: 2, info: [{ idx: 1, qty: 1 }, { idx: 2, qty: 2 }] },
    { id: 2, total: 1, info: [{ idx: 1, qty: 1 }] }
];
var ob2 = [
    { id: 1, total: 1, info: [{ idx: 1, qty: 1 }] },
    { id: 2, total: 4, info: [{ idx: 1, qty: 2 }, { idx: 2, qty: 2}] }
];

console.log('ob1', checkTotal(ob1));
console.log('ob2', checkTotal(ob2));

【讨论】:

    【解决方案2】:

    您可以通过使用reduce()比较总数和获取信息总数qtyfilter记录。这是一个工作示例

    var ob1 =[
     {id:1, total: 2, info:[{idx:1, qty:1},{idx: 2, qty: 2}] },
     {id:2, total:1, info: [{idx:1, qty:1}]}
    ];
    
    var result = ob1.filter(k=>k.total!=k.info.reduce((a, {qty})=>a+qty,0));
    
    console.log(result);

    我希望这会引导你走向正确的方向。

    【讨论】:

    • @Andreas 更新了我的答案。看看吧。
    【解决方案3】:

    工作注释示例:

    // finds total sum of qtys in info array
    function totalSum(infoArray) {
      return infoArray.reduce((acc, info) => acc + info.qty, 0);
    }
    
    // returns items with incorrect totals
    function checkTotal(itemArray) {
      return itemArray.filter(item => item.total !== totalSum(item.info));
    }
    
    let ob1 = [
      {id:1, total:2, info:[{idx:1, qty:1}, {idx:2, qty:2}]}, // incorrect total
      {id:2, total:1, info:[{idx:1, qty:1}]}, // correct total
    ];
    
    let ob2 = [
      {id:1, total:1, info:[{idx:1, qty:1}]}, // correct total
      {id:2, total:4, info:[{idx:1, qty:2}, {idx: 2, qty: 2}]}, // correct total
    ];
    
    console.log(checkTotal(ob1)); // returns 1st item
    console.log(checkTotal(ob2)); // returns empty array

    【讨论】:

      猜你喜欢
      • 2018-02-06
      • 2016-01-24
      • 1970-01-01
      • 1970-01-01
      • 2021-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多