【问题标题】:How to check the length of array of objects here object is having array inside?如何检查对象数组的长度这里对象内部有数组?
【发布时间】:2019-05-19 17:58:59
【问题描述】:

以下是我的数据

var a=[
        {
            name: "time",
            Details:[
                {value:"month",checked:true,id:1}
            ]
        },
        {
            name: "product",
            Details:[
                {value:"abc",checked:true,id:2},
                {value:"abx",checked:false,id:3}
            ]
        }
    ]

我想找到已检查的计数。在示例中计数为 2 如何以优化的方式进行(没有 for 循环,没有过滤器的过滤器)。

【问题讨论】:

标签: javascript angular typescript


【解决方案1】:

您可以结合几个函数来获取所有带有检查值的详细信息:

a.map(t => t.Details).flat().filter(t=>t.checked)

先做一个map,然后把结果扁平化成一个数组,然后按checked过滤

【讨论】:

  • 而且,如果您需要计数,只需添加长度:a.map(t => t.Details).flat().filter(t=>t.checked).length
【解决方案2】:

您可以在此处使用标准减速器。

const a=[
  {
    name: "time",
    Details:[
    {value:"month",checked:true,id:1}
    ]
  },
  {
    name: "product",
    Details:[
    {value:"abc",checked:true,id:2},
    {value:"abx",checked:false,id:3}
    ]
  }
]


const total = a.reduce((total, entry) => total + entry.Details.reduce((all, inner) => all + (inner.checked === true ? 1 : 0), 0), 0)

console.log(total);

再一次,以更易读的方式:

const total = a.reduce((total, item) => {
  // add the total to a result of adding up items in the inner arra
  return total + item.Details.reduce((innerTotal, entry) => {
    // for inner item, we add 1 if the entry is checked.
    if (entry.checked) {
      return innerTotal + 1;
    }
    // otherwise, just return the current total.
    return innerTotal;
  }, 0), // innerTotal starts at 0
 }, 0);  // and so does the outer total.

【讨论】:

  • 我会做一个过滤器而不是嵌套的 reduce:reduce( (sum, { Details }) => sum + Details.filter((detail) => detail.checked).length, 0, );
  • Op 说没有过滤器。
  • 那么 OP 就没有意义了。也许OP认为过滤器会变异?通常当有人问What's the best way to jump off a high building之类的问题时,我会尝试回答take the elevator instead
  • 可能。无论如何,这两种解决方案都可以正常工作,可能与任何解决方案一样高效。可能还有其他几种方法可以做到这一点。
  • 非常感谢@Zlatko 提供了很棒的解决方案
【解决方案3】:

试试这个,希望对你有用

    let Count=0;
var a=[
            {
                name: "time",
                Details:[
                    {value:"month",checked:true,id:1}
                ]
            },
            {
                name: "product",
                Details:[
                    {value:"abc",checked:true,id:2},
                    {value:"abx",checked:false,id:3}
                ]
            }
        ];
a.map(function(value,index){
value.Details.map(function(innerValue, innerIndex){
if( innerValue.checked==true){
Count++;
}
});
}); 

【讨论】:

  • 传递给map的函数通常是纯函数。如果您想更改 map 函数范围之外的变量(=副作用,所以不纯),请改用 forEach。
  • @faraz 感谢您在您的解决方案中回答,我想避免的回调数量更多
【解决方案4】:

您需要在嵌套模式中使用两个forEach()

var a = [{
    name: "time",
    Details: [{
      value: "month",
      checked: true,
      id: 1
    }]
  },
  {
    name: "product",
    Details: [{
        value: "abc",
        checked: true,
        id: 2
      },
      {
        value: "abx",
        checked: false,
        id: 3
      }
    ]
  }
];

var count = 0;
a.forEach(({Details}) => Details.forEach(({checked}) => {
  if(checked){
    count++;
  }
}))
console.log(count);

【讨论】:

  • 这对我有帮助。有没有办法减少回调次数?
  • @ravireddy 不,无论如何你都需要两个嵌套循环
猜你喜欢
  • 2019-05-28
  • 1970-01-01
  • 2020-11-02
  • 2019-12-11
  • 2013-12-19
  • 1970-01-01
  • 1970-01-01
  • 2011-08-01
相关资源
最近更新 更多