[英]JS reduce - sum only array lengths in object
I have the following data structure: 我有以下数据结构:
const arr = [
{key: 0, x: [], y: []},
{key: 0, x: [], y: []}
]
I want to check if all arrays are empty, I have how can I do this using reduce? 我想检查所有数组是否为空,我有怎样才能使用reduce? My code so far: 我的代码到目前为止:
arr.reduce((count, acc) => {
return acc !== 'key' ? count + acc.length : count
}, 0)
You could take key
out of the object and check all length for having zero. 您可以从对象中取出key
并检查所有长度是否为零。
const array = [{ key: 0, x: [], y: [] }, { key: 0, x: [], y: [] }], allZero = array.every(({ key, ...o }) => Object.values(o).every(({ length }) => !length)); console.log(allZero);
Summing 总结
const array = [{ key: 0, x: [], y: [] }, { key: 0, x: [], y: [] }], sum = array.reduce((s, { key, ...o }) => Object.values(o).reduce((t, { length }) => t + length, s), 0); console.log(sum);
reduce
is a bit of a clunky way to do this, and you're loopin over the items in the array, not the keys as (I think) you expected. reduce
是一种笨重的方式,你可以循环遍历数组中的项目,而不是你想要的键(我认为)。
Use Array.every
instead: 请改用Array.every
:
const arr = [ {key: 0, x: [], y: []}, {key: 0, x: [], y: []} ] const arr2 = [ {key: 0, x: [1,2,3], y: []}, {key: 0, x: [], y: []} ] function areAllEmpty(a){ return a.every(i => ixlength == 0 && iylength == 0); } console.log(areAllEmpty(arr)); console.log(areAllEmpty(arr2));
Because acc
will be the object in the array, you should check the object properties which are arrays. 因为acc
将是数组中的对象,所以应该检查作为数组的对象属性。 Use Object.values
for this purpose, as well as another reduce
for the arrays inside each object, and exclude key
: 为此目的使用Object.values
,以及每个对象内的数组的另一个reduce
,并排除key
:
const arr = [ {key: 0, x: [], y: []}, {key: 0, x: [], y: []} ]; const res = arr.reduce((a, { key, ...c }) => a + Object.values(c).reduce((e, { length: f }) => e + f, 0), 0); console.log(res);
You can use filter()
on Object.values
to get only array values. 您可以在Object.values
上使用filter()
来仅获取数组值。 And then flat()
and add the length to result 然后flat()
并将结果添加长度
const arr = [ {key: 0, x: [], y: []}, {key: 0, x: [], y: []} ] let res = arr.reduce((count, acc) => { return count + Object.values(acc).filter(x => x.constructor === Array).flat().length }, 0) console.log(res)
Sum only arrays' lengths in object using Array.reduce()
: 使用Array.reduce()
仅对对象中的数组长度求和:
const arr = [ {key: 0, x: [], y: [], abc: "hsgg" }, {key: 0, x: [1, 2, 3, 4], y: [1, 2]} ]; const c = arr.reduce((count, obj) => { Object.values(obj).forEach(v => { if(Array.isArray(v)) count += v.length; }); return count; }, 0); console.log(c);
Note that in the other answers strings' lengths are also counted 请注意,在其他答案中, 字符串的长度也会计算在内
Array.every
is the better option. Array.every
是更好的选择。 You can iterate over all the values of the objects and test to see if they're arrays and, if they are, return false
if they're not empty. 您可以遍历对象的所有值并进行测试以查看它们是否为数组,如果它们是数组,则返回false
如果它们不为空)。 every
will stop as soon as it finds a non-empty array , or end the of the iteration, whichever comes first. every
它找到一个非空数组 ,或者结束迭代时, every
都会停止,以先到者为准。
const arr = [ {key: 0, x: [], y: []}, {key: 0, x: [], y: []} ]; const arr2 = [ {key: 0, x: [], y: [1]}, {key: 0, x: [], y: []} ]; const allEmpty = (arr) => arr.every(el => { for (let v of Object.values(el)) { if (Array.isArray(v) && v.length > 0) return false; } return true; }); console.log(allEmpty(arr)); console.log(allEmpty(arr2));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.