I have an object that looks like this:
let arr = {
names: ['John', 'Paul'],
phones: ['iPhone', 'Samsung', 'Huawei'],
clothesBrands: ['HM', 'Zara']
}
I have an object that looks like this:
let arr = {
names: ['John', 'Paul'],
phones: ['iPhone', 'Samsung', 'Huawei'],
clothesBrands: ['HM', 'Zara']
}
I need to sum the lengths of the arrays. I can do it like this:
arr.names.length + arr.phones.length + arr.clothesBrands.length
and the result is 7.
How can I do it in a clean way if I don't know all the keys?
Share Improve this question edited Feb 22, 2019 at 5:23 adiga 35.3k9 gold badges65 silver badges87 bronze badges asked Feb 22, 2019 at 5:18 Tom BomTom Bom 1,7214 gold badges20 silver badges45 bronze badges 1- Will the key values always be an array? – Spangle Commented Feb 22, 2019 at 5:37
7 Answers
Reset to default 7You can check the length of the .flat
tened array of values:
const arr = {
names: ['John', 'Paul'],
phones: ['iPhone', 'Samsung', 'Huawei'],
clothesBrands: ['HM', 'Zara']
};
console.log(Object.values(arr).flat().length);
You can use object.values and reduce
Object.values
will give you all the values in Object and with reduce
we calculate total length.
let obj = {names: ['John', 'Paul'], phones: ['iPhone', 'Samsung', 'Huawei'], clothesBrands: ['HM', 'Zara']}
let op = Object.values(obj).reduce( (out, inp) => out + inp.length, 0)
console.log(op)
Use reduce . Get all the values in the objects and in reduce add the lengths of the arrays one by one
var arr = {
names: ['John', 'Paul'],
phones: ['iPhone', 'Samsung', 'Huawei'],
clothesBrands: ['HM', 'Zara']
}
console.log(Object.values(arr).reduce((acc, e) => acc += e.length, 0))
You can use Array.prototype.reduce()
on Object.values
and check whether the value is array by Array.isArray()
let obj ={names: ['John', 'Paul'], phones: ['iPhone', 'Samsung', 'Huawei'], clothesBrands: ['HM', 'Zara']}
let sum = Object.values(obj).reduce((ac,a) => {
return Array.isArray(a) ? ac + a.length : ac
},0)
console.log(sum);
For in loops are your friend when working with objects. Example to solve your problem below:
var arr = {names: ['John', 'Paul'], phones: ['iPhone', 'Samsung', 'Huawei'], clothesBrands: ['HM', 'Zara'], hello: 1};
var totalLength = 0;
for(var char in arr){
// Confirm that the key value is an array before adding the value.
if(Array.isArray(arr[char])){
totalLength += arr[char].length;
}
}
console.log(totalLength);
Hopefully this helps.
Here you have another alternative using for ... in to iterate over the object keys
directly:
let arr = {
names: ['John', 'Paul'],
phones: ['iPhone', 'Samsung', 'Huawei'],
clothesBrands: ['HM', 'Zara']
}
let totalLen = 0;
for (k in arr)
{
Array.isArray(arr[k]) && (totalLen += arr[k].length);
}
console.log(totalLen);
You should use Array.reduce
for this.
const arr = {names: ['John', 'Paul'], phones: ['iPhone', 'Samsung', 'Huawei'], clothesBrands: ['HM', 'Zara']};
const sum = Object.values(arr).reduce((acc, {length}) => {
return acc = length ? acc+ length : acc;
}, 0);
console.log(sum);