1

This question is extremely similar to:
generate object from key array and values array
Transform array of keys and array of values into array of objects javascript

This question has been answered more than once and might seem like a duplicate. It is possible to construct an object from arrays of keys and values. However, every method for such a construction uses loops or function like .reduce() that utilize loops. Deconstructing an object is can be easily done using inbuilt methods Object.keys() and Object.values() and Object.entries(). Constructing an object from key-value pairs is also possible using Object.fromEntries(). The issue is that given two arrays, one of keys and another of values, is it possible without loops to construct an object.

(Hopefully the sample code below clarify what my complicated question doesn't. PR might not be my strong suit ¯\_(ツ)_/¯)

function constructObject (keys, values)
{
  // ... loopless implementation
}

constructObject(["a", "b", "c", Symbol("d")], [0, null, false, {}]);
/*
// expected output
{
"a" : 0,
"b" : null,
"c" : false,
[Symbol("d")] : {}
}
*/
5
  • everything you mentioned is a loop internally i guess Commented Aug 1, 2022 at 19:03
  • A low level loop is not as bad as a for loop in js ig Commented Aug 1, 2022 at 19:04
  • 2
    you are underestimating for loops Commented Aug 1, 2022 at 19:05
  • 1
    youmightnotneed.com/lodash#zipObject Commented Aug 1, 2022 at 19:25
  • Still uses a loop but i guess that is inevitable Commented Aug 1, 2022 at 19:27

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.