I have object like this (nodename)
{
BGF: [
'bgf-simulator-pn',
'bgf-simulator',
'bgf-simulator-2',
'bgf-1-error'
],
Baseband: [ 'baseband_2', 'baseband_1' ],
CCPC: [ 'ccpc-emulator' ],
CCRC: [ 'ccrc-emulator' ],
CEE: [ 'cee' ],
}
I want get length of array key
I used this way
const nodename = await topologyBrowser.nodes();
const arrayLength = Object.keys(nodename).length;
console.log(arrayLength);
and console log return 5, but I want 9,
what did I wrong, what should I do guys
You can make use of Object#values() and use Array#flat() to flattern the multi-dimensional array.
Get the length like this: Object.values(nodename).flat().length!
See a working example-snippet below:
const nodename = {
BGF: [
'bgf-simulator-pn',
'bgf-simulator',
'bgf-simulator-2',
'bgf-1-error'
],
Baseband: ['baseband_2', 'baseband_1'],
CCPC: ['ccpc-emulator'],
CCRC: ['ccrc-emulator'],
CEE: ['cee'],
};
const res = Object.values(nodename).flat().length;
console.log(res)
You can make use of Array#reduce() method:
const nodename = {
BGF: [
'bgf-simulator-pn',
'bgf-simulator',
'bgf-simulator-2',
'bgf-1-error'
],
Baseband: [ 'baseband_2', 'baseband_1' ],
CCPC: [ 'ccpc-emulator' ],
CCRC: [ 'ccrc-emulator' ],
CEE: [ 'cee' ],
};
const result = Object.values(nodename).reduce((res, curr) => res + curr.length, 0);
console.log(result);