-1

At the entrance I have such an array with objects. Function that converts an incoming array of objects into an object. Using the function, I need to bring it to this form.

var array = [ 
   { k1:v1 },
   { k2:v2 },
   { k3:v3 }
];

function arrayToObject(array) { return object }

var object = { 
    v1: k1,
    v2: k2,
    v3: k3, 
}
  • There are so many answers out there that can help: https://stackoverflow.com/questions/19590865/from-an-array-of-objects-extract-value-of-a-property-as-array for example – mplungjan Feb 02 '19 at 15:21
  • 1
    Wait, do you really need to swap keys with values? – Bergi Feb 02 '19 at 15:31

7 Answers7

2

You could taske Object.assign and spread the reversed objects.

var array = [ { k1: 'v1' }, { k2: 'v2' }, { k3: 'v3' }],

object = Object.assign(...array.map(o => Object
    .entries(o)
    .reduce((r, [k, v]) => Object.assign(r, { [v] : k }), {})
));

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

Use forEach loop

var array = [ 
   { k1:'v1' },
   { k2:'v2' },
   { k3:'v3' }
]

function a()
{
var obj={};
array.forEach((e)=>obj[e[Object.keys(e)[0]]]=Object.keys(e)[0])
console.log(obj)
}
a();
ellipsis
  • 12,049
  • 2
  • 17
  • 33
  • Could you explain this piece of code? @AshayMandwarya – Илия Полторацкий Feb 03 '19 at 21:21
  • The forEach loop is used to itearte over an array one by one. the e on every iteration represents the object in the array. `Object.keys(e)[0]` this gives us an array of all the keys of the object e. There is only one so we use [0] to get that key. We access the value corresponding to that key using e[key] we get the key from the above `Object.keys(e)[0]` accessing this gives us the value corresponding to that key. Now we have the value, inside the empty object `obj` we make this as the key again using [] `obj[key]` and this key is given the value of `Object.keys(e)[0]` which is the key itself – ellipsis Feb 03 '19 at 21:35
  • Thus interchanging the key and value. Hope you understood – ellipsis Feb 03 '19 at 21:35
  • T u so much @AshayMandwarya – Илия Полторацкий Feb 03 '19 at 22:43
1

You can use Object.entries() and .reduce() methods to get the desired output:

const array = [ 
   { k1:'v1' },
   { k2:'v2' },
   { k3:'v3' }
];

const obj = Object.entries(
    array.reduce((r, c) => Object.assign(r, c), {})
).reduce((r, [k, v]) => (r[v] = k, r), {});

console.log(obj);
Mohammad Usman
  • 37,952
  • 20
  • 92
  • 95
1

Array.reduce and use Object.keys over each array element.

var array = [ 
   { k1: 'v1' },
   { k2: 'v2' },
   { k3: 'v3' }
]

var obj = array.reduce((obj, item) => {
  Object.keys(item).forEach(key => obj[item[key]] = key)
   
  return obj
}, {})

console.log(obj)
nicholaswmin
  • 21,686
  • 15
  • 91
  • 167
0

And another one:

 const result = {};

 for(const [[key, value]] of array.map(Object.entries))
   result[value] = key;
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
-1

I am not sure why the other answers go through hoops to make this as clever as possible.

I find this more readable. I am not using reduce because I find the word misleading. A simple forEach makes more sense to me

const array = [ 
   { k1:'v1' },
   { k2:'v2' },
   { k3:'v3' }
];
let newObj={};

array.forEach((obj) => { 
  let key = Object.keys(obj)[0];
  newObj[obj[key]]=key;
})  
console.log(newObj)
mplungjan
  • 169,008
  • 28
  • 173
  • 236
-2

your answer..

var array = [
  { k1: v1 },
  { k2: v2 },
  { k3: v3 }
];

function arrayToObject(array) {
  obj = {};
  for (i = 0; i < array.length; i++) {
    o = array[i];
    key = Object.keys(o)[0];
    obj.key = o.key;
  }
  return obj;
}


console.log(arrayToObject(array))
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Ande Caleb
  • 1,163
  • 1
  • 14
  • 35