计算每个对象的长度属性. [英] Count length properties of each object.entry
问题描述
所以我希望能够计算数组中每个对象内的属性数量...
So I want to be able to count the number of properties within each object in an array...
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.
Basically I want to do this to check for a change. If more than 3 properties in any of the objects. I know how to count the number of objects, in this case there are 3. But need to count the properties within. If more than 3 return true.
我正在努力寻找超出对象问题计数范围之外的任何东西.我正在使用lodash(如果有用的话).
I am struggling to find anything that gets past the counting of Objects question. I am using lodash if useful for an answer.
推荐答案
将数组映射到每个对象的 Object.keys()
并检查是否大于3:
Map the array to the length of the Object.keys()
of each object and check if greater than 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()
以获得每个对象中的属性数量,然后使用 <代码> _.lt() :
Or use lodash's _.size()
to get the number of properties in each object, and then check if 3 is less than the number with _.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>
这篇关于计算每个对象的长度属性.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!