javascriptecmascript-2017

Good ways to convert 2d array to object


With ES7 ES2017 (ES8) I can convert an object to an array of arrays like this:

let obj = { a: '1', b: '2', c: '3' }
let arr = Object.entries(obj)
// [ [ 'a', '1' ], [ 'b', '2' ], [ 'c', '3' ] ]

But what if I want to go the other way around? I could use a for-loop, or the reduce-method of the Array like below, but is there a more direct approach?

let arr =[ [ 'a', '1' ], [ 'b', '2' ], [ 'c', '3' ] ]
let obj = arr.reduce((o, x)=>{o[x[0]]=x[1];return o}, {})
// { a: '1', b: '2', c: '3' }

A use case: I wanted to convert a string like "a=1;b=2;c=3" to an object like { a: '1', b: '2', c: '3' } and ended up with something like

"a=1;b=2;c=3"
  .split(";")
  .reduce((o, x)=>{
    let parts=x.split("=")
    o[parts[0]]=parts[1]
    return o
  }, {});

Edit: As pointed out in comments, the reduce syntax could be simplified (or shortened, at least) to:

arr.reduce((o, [k, v])=>(o[k]=v, o), {})

...or written with Object.assign like this:

arr.reduce((o, [k, v])=>Object.assign(o, {[k]: v}), {})

Either way, the answer to the question, according to the comments, is no. This is the best approach we've got.

Edit, Jan 2023: .fromEntries() is in stage 4 since 2019, and now supported in all modern browsers.


Solution

  • Do you want some one-line unentries function?

    const detries = entries => entries.reduce((result, [key, value]) => Object.assign(result, {[key]: value}), {});
    
    const entries = Object.entries({a: 1, b: 2, c: 3});
    const detried = detries(entries);
    
    console.info("ENTRIES", entries);
    console.info("DETRIED", detried);


    Edit: Object.fromEntries proposal.

    You will probably be able to use this one instead.

    Object.fromEntries is proposed to perform the reverse of Object.entries: it accepts an iterable of key-value pairs and returns a new object whose own keys and corresponding values are given by those pairs.

    Object.fromEntries([["apple", 0], ["banana", 1]]) // {apple: 0, banana: 1}