1
    var test  = [
      {test1: 1, test2: 2},
      {test3: 3}
    ];
    console.log(test[0].length);

Im basically trying to find a new to find the length of for example test[0] which would be 2, and test[1] which would be 1. Im not good at javascript and would need to know this for a school profect

  • Hi! Could you please rephrase your questions? It's not clear exactly what you want to know/learn. – kunambi Apr 27 '20 at 23:40

1 Answers1

0

Here is a solution that will give you the length for every object, no matter how many objects are in your Array.

The length of the first object will be at lengths[0], the second one at lengths[1], so on and so forth.

let test  = [
  {test1: 1, test2: 2},
  {test3: 3},
  {test4: 4, test5: 5, test6: 6}
];

let lengths = test.map(obj => Object.keys(obj).length)

console.log(lengths)

This may seem a little cryptic if you're not comfortable using .map so I recommend you read about it. Basically it runs a function on every item in your array and returns an array with the results of that function.

Félix Paradis
  • 5,165
  • 6
  • 40
  • 49