Evanss
Evanss

Reputation: 23173

Javascript map to Associative Array?

I have an array of objects like so:

const content = [
    {
        title: 'Morning run',
        id: 'id1',
        desc: 'Meet at the park',
        date: '2018-01-14T09:00:00.000Z',
        location: 'Central park',
        createdBy: '23432432',
    },
    {
        title: 'Evening run',
        id: 'id2',
        desc: 'Meet by the station',
        date: '2018-01-14T18:00:00.000Z',
        location: 'Central station',
        createdBy: '23432432',
    },
];

How can I create an associative array like so?:

const output = {'id1' : 'Morning run', 'id2' : 'Evening run'}

Can this be done with a map function?

Upvotes: 6

Views: 17469

Answers (5)

Hassan Imam
Hassan Imam

Reputation: 22574

Use array#map with Object#assign to create an object with id and title.

const content = [{ title: 'Morning run', id: 'id1', desc: 'Meet at the park', date: '2018-01-14T09:00:00.000Z', location: 'Central park', createdBy: '23432432', }, { title: 'Evening run', id: 'id2', desc: 'Meet by the station', date: '2018-01-14T18:00:00.000Z',location: 'Central station', createdBy: '23432432', }, ],
      result = Object.assign(...content.map(({id, title}) => ({[id]: title})));

console.log(result);

One can also use Object.fromEntries() with array#map to create the object.

Object.fromEntries(content.map(({id, title}) => ([id, title])))

const content = [{ title: 'Morning run', id: 'id1', desc: 'Meet at the park', date: '2018-01-14T09:00:00.000Z', location: 'Central park', createdBy: '23432432', }, { title: 'Evening run', id: 'id2', desc: 'Meet by the station', date: '2018-01-14T18:00:00.000Z',location: 'Central station', createdBy: '23432432', }, ],
      result = Object.fromEntries(content.map(({id, title}) => ([id, title])));

console.log(result);

Upvotes: 7

Prakash Sharma
Prakash Sharma

Reputation: 16482

Use Array.reduce function like this

let out = content.reduce(function(a,b){
   a[b.id] = b.title
   return a;
},{})

Upvotes: 5

Robdll
Robdll

Reputation: 6263

    let initVal = {};
    let content = [
    {
        title: 'Morning run',
        id: 'id1',
        desc: 'Meet at the park',
        date: '2018-01-14T09:00:00.000Z',
        location: 'Central park',
        createdBy: '23432432',
    },
    {
        title: 'Evening run',
        id: 'id2',
        desc: 'Meet by the station',
        date: '2018-01-14T18:00:00.000Z',
        location: 'Central station',
        createdBy: '23432432',
    },
];

    content = content.reduce(function(myObj,next) { 
        myObj[next.id] = next.title;
        return myObj; 
     }, initVal );

console.log(content);

Upvotes: 0

Jonas Wilms
Jonas Wilms

Reputation: 138537

const map = {};
for(const {id, title} of content)
   map[id] = title;

Just iterate over your content and add every entry to the map.

Upvotes: 2

Nisarg Shah
Nisarg Shah

Reputation: 14561

Since you need just one object in the result, you could use array#reduce like this:

const content = [{
    title: 'Morning run',
    id: 'id1',
    desc: 'Meet at the park',
    date: '2018-01-14T09:00:00.000Z',
    location: 'Central park',
    createdBy: '23432432',
  },
  {
    title: 'Evening run',
    id: 'id2',
    desc: 'Meet by the station',
    date: '2018-01-14T18:00:00.000Z',
    location: 'Central station',
    createdBy: '23432432',
  },
];

var result = content.reduce(function(accum, currentVal) {
  accum[currentVal.id] = currentVal.title;
  return accum;
}, {});

console.log(result);

Upvotes: 13

Related Questions