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")] : {}
}
*/