Answers for "javascript array to object with keys"

5

js array into object

const names = ['Alex', 'Bob', 'Johny', 'Atta'];

// convert array to th object
const obj = Object.assign({}, names);

// print object
console.log(obj);

// {0: "Alex", 1: "Bob", 2: "Johny", 3: "Atta"}
Posted by: Guest on October-25-2020
34

object keys javascript

const object1 = {
  a: 'somestring',
  b: 42,
  c: false
};

console.log(Object.keys(object1));
// expected output: Array ["a", "b", "c"]
Posted by: Guest on March-12-2020
1

javascript array to object with keys

let arr = [{a: 'a', b: 1, c: 'c'}, {a: 'a', b: 2, c: 'c'}, {a: 'a', b: 3, c: 'c'}]:
 let mapped = arr.reduce (function (map, obj) { 
        map[obj.b] = obj; 
        return map;
      },{}); // reduce
console.log (mapped); // {1: {a: 'a', b: 1, c: 'c'}, 2: {a: 'a', b: 2, c: 'c'}, 3: {a: 'a', b: 3, c: 'c'}
Posted by: Guest on February-18-2021
6

convert array to object javascript

const convertArrayToObject = (array, key) =>
  array.reduce(
    (obj, item) => ({
      ...obj,
      [item[key]]: item
    }),
    {}
  );
Posted by: Guest on May-05-2020
0

js array to object with keys

const arr = ['a','b','c'];
const res = arr.reduce((a,b)=> (a[b]='',a),{});
console.log(res)
Posted by: Guest on June-02-2020
1

es6 array to object keys

const subLocationTypes = (location.subLocationTypes || []).reduce((add, cur) => {
add[cur.key] = cur.value;
return add;
}, {});
Posted by: Guest on October-28-2020

Code answers related to "javascript array to object with keys"

Code answers related to "Javascript"

Browse Popular Code Answers by Language