-1

I can achieve this using the for loop but I cant figure out how to use forEach to do the same.

for example: var input = [['make', 'Ford'], ['model', 'Mustang'], ['year', 1964]]; calling the function should result in:

{
  make : 'Ford',
  model : 'Mustang',
  year : 1964
}
Randy Casburn
  • 13,840
  • 1
  • 16
  • 31
  • Why are you limited to `forEach`? – Heretic Monkey Apr 01 '21 at 16:40
  • [Convert Array to Object](https://stackoverflow.com/q/4215737/215552) is the canonical duplicate. If your answer is the same as the answers there (i.e., uses `reduce`) DO NOT ANSWER THIS QUESTION. This question asks about using `forEach`. – Heretic Monkey Apr 01 '21 at 16:46
  • 1
    Call [`Object.fromEntries()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/fromEntries) with the array as parameter. – adiga Apr 01 '21 at 16:46

2 Answers2

1

We can achieve this using forEach and Array destructuring

var input = [
  ["make", "Ford"],
  ["model", "Mustang"],
  ["year", 1964],
];

const result = {};

input.forEach(([prop, value]) => {
  result[prop] = value;
});

/*--------- OR --(withour array destructuring)-------------
input.forEach(internalArr => {
  result[internalArr[0]] = internalArr[1];
});
*/

console.log(result);

You can also achieve this using Array.prototype.reduce. Array.prototype.reduce is the right tool to do this job.

var input = [
  ["make", "Ford"],
  ["model", "Mustang"],
  ["year", 1964],
];

const result = input.reduce((acc, curr) => {
  const [prop, value] = curr;
  acc[prop] = value;
  return acc;
}, {});

console.log(result);
DecPK
  • 24,537
  • 6
  • 26
  • 42
  • 1
    We have many duplicates of "better options" using `reduce` among others solutions. This question asks for answers using `forEach`. – Heretic Monkey Apr 01 '21 at 16:48
  • @HereticMonkey added the solution with forEach also – DecPK Apr 01 '21 at 16:49
  • I'd say `Array.prototype.reduce` is not *the* right tool. It doesn't differ from other loop construct, really. – apple apple Apr 01 '21 at 16:54
  • @appleapple I'd love if you can explain me, why `Array.prototype.reduce` is not the right tool. May be there is something that I don't know. So that I can learn something new from you. ☺☺☺ – DecPK Apr 01 '21 at 16:55
  • @DeC it's why I highlight *the*. I just mean it doesn't have real benefit over other loop construct. – apple apple Apr 01 '21 at 16:57
  • @DeC and yes, [adiga post an build-in well-named function at origin post](https://stackoverflow.com/questions/66908398/how-can-i-use-foreach-to-transform-a-nested-array-into-an-object#comment118271076_66908398) – apple apple Apr 01 '21 at 16:57
  • @appleapple we need to use for loop for `Object.entries`. OP is asking for `forEach`. It is exactly same as like I've given the alternative soluting using `Array.prototype.reduce`. – DecPK Apr 01 '21 at 17:01
  • It's a duplicate of [How to create an object from an Array of key-value pairs?](https://stackoverflow.com/questions/20059995) and many other questions. The duplicate has forEach, for, reduce, lodash, Object.fromEntries etc – adiga Apr 01 '21 at 17:02
  • @DeC yes that's my point, I think the phrase *is the right tool* is inappropriate in this answer since it has no real benefit over other loops. (or other well named functions) – apple apple Apr 01 '21 at 17:03
0

You could take a closure for the target object and iterate the array with the returned function from convert.

const
    convert = target => ([key, value]) => target[key] = value,
    input = [['make', 'Ford'], ['model', 'Mustang'], ['year', 1964]],
    output = {};

input.forEach(convert(output));

console.log(output);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392