所以我希望能够计算数组中每个对象内的属性数量...
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(如果有用的话)。
答案 0 :(得分:2)
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>
答案 1 :(得分:0)