Task:
Write a function that converts an incoming array of objects into an object. The keys (key) of the returned object should be the values ​​of the elements of the original array, the values ​​of the returned ones should be the keys of the original.

Examples:

input: [ { k1:v1 }, { k2:v2 }, { k3:v3 } ] output: { v1:k1, v2:k2, v3:k3, } 

Tried it like this, only output array

 function arrayToObject (array) { array.reduce(function (result, item, index) { var key = Object.keys(item)[0]; var value = item[key]; var obj = {}; obj[value] = key; result.push(obj); return result; }, []); } 
  • tried it yourself? - Stranger in the Q
  • Tried it like this, just output the array: function arrayToObject (array) {array.reduce (function (result, item, index) {var key = Object.keys (item) [0]; var value = item [key]; var obj = {}; obj [value] = key; result.push (obj); return result;}, []); } - Junior viewer

3 answers 3

The problem with the above code is that, firstly, the function returns nothing, and secondly, the initial value was an array. Therefore, it is strange to expect an object at the exit.

After correcting these errors, you can go to the convolution algorithm.

In fact, inside one reduce should be located the second one, which would add values ​​from the current object to the resulting one.

To do this, you can use both the .keys method .keys returns an array of keys, which can then be collapsed into an object, specifying the initial value in result , and the .entries method .entries which also returns an array whose elements are a key-value pair.

The final code might look like this:

 var t = [{ k1: 'v1' }, { k2: 'v2' }, { k3: 'v3' }]; function arrayToObject(array) { return array.reduce(function(result, item) { return Object.keys(item).reduce((r, key) => { result[item[key]] = key; return result; }, result); }, {}); } console.log(arrayToObject(t)); 

Or so:

 var t = [{ k1: 'v1' }, { k2: 'v2' }, { k3: 'v3' }]; function arrayToObject(array) { return array.reduce(function(result, item) { return Object.entries(item).reduce((r, [key, value]) => Object.assign(result, { [value]: key }), result); }, {}); } console.log(arrayToObject(t)); 

    It is possible so:

     function revert(input) { return input.reduce((s, c) => { Object.entries(c).map(function(item) { let [value, key] = item; s[key] = value; }); return s; }, {}) }; let test = [ { k1: "v1" }, { k2: "v2" }, { k3: "v3" } ]; console.log(revert(test)); 

    • Thank you so much for your help, as a result, and took your option.)) - Junior viewer

     const arr = [{ k1: 'v1' }, { k2: 'v2' }, { k3: 'v3' }]; const result = Object.fromEntries(arr.map(obj => Object.entries(obj)[0].reverse())); console.log(JSON.stringify(result)); 

    The Object.fromEntries() method is already supported by current versions of Chrome and Firefox (including mobile).
    It will become standard this year - with the release of ES10.

    Polyfill
    + ponifill (without the support of IE, etc.)

    • Which browsers support this feature? - Grundy