gpt4 book ai didi

javascript - 计算每个 object.entry 的长度属性

转载 作者:行者123 更新时间:2023-11-30 14:10:07 25 4
gpt4 key购买 nike

所以我希望能够计算数组中每个对象的属性数量...

    value = [
0: { personId: "0003678", seniorStatus: "Yes", juniors: "maybe" }, //3
1: { personId: "0001657", seniorStatus: "No", juniors: "No" }, //3
2: { personId: "0002345", seniorStatus: "No", juniors: "No", infants: "Maybe" } //4

基本上我想这样做是为了检查是否有变化。如果任何对象中的属性超过 3 个。我知道如何计算对象的数量,在本例中有 3 个。但需要计算其中的属性。如果超过 3 个则返回 true。

我正在努力寻找任何可以通过对象计数问题的东西。如果对答案有用,我正在使用 lodash。

最佳答案

Map数组到 Object.keys() 的长度每个对象并检查是否大于 3:

const values = [{"personId":"0003678","seniorStatus":"Yes","juniors":"maybe"},{"personId":"0001657","seniorStatus":"No","juniors":"No"},{"personId":"0002345","seniorStatus":"No","juniors":"No","infants":"Maybe"}]

const result = values.map(o => Object.keys(o).length > 3)

console.log(result)

或者使用 lodash 的 _.size()获取每个对象中的属性数量,然后检查 3 是否小于带 _.lt() 的数量:

const values = [{"personId":"0003678","seniorStatus":"Yes","juniors":"maybe"},{"personId":"0001657","seniorStatus":"No","juniors":"No"},{"personId":"0002345","seniorStatus":"No","juniors":"No","infants":"Maybe"}]

const result = values.map(_.flow(
_.size,
_.partial(_.lt, 3)
))

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>

关于javascript - 计算每个 object.entry 的长度属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54620211/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com