Javascript Array reduce

Hi, i have a Javascript problem:

Input:

var arr = [
  ["name", "Stell"],
  ["age", 18],
];

Output:

console.log(arrToObj(arr)); // { name: 'Steel', age: 18 }

Code:

function arrToObj(arr) {
  const result = arr.reduce(function (total, item) {
     const key = item[0];
     const value = item[1];
     const newObj = { [key]: value };
     return total.concat(newObj);
  }, [   ]);
  return {...result[0],...result[1]};
}

why here:

index[0]= name, age
index[1]= Steel, 18

???
I haven’t flattened the original array yet so:

index[0]=['name', 'Steel']
index[1]=['age', 18]

Which answer is correct?
Please help me explain this topic!
Thanks.

Is this question about a specific fCC challenge? Can you link to a description of the problem?

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

Not sure I understand the question but item is each of the nested arrays and item[someIndex] is each element inside the nested arrays.

I assume by index[0] and index[1] you are referring to item[0] and item[1]

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.