Javascript中map如何转换为关联数组?

6

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'}

这可以使用map函数完成吗?

3
首先,一个小注释:JavaScript 中并不存在关联数组,你建议的 output 表示法实际上是一个对象。请注意,我的翻译没有改变原意,同时使内容更加易懂。 - rolfv1
5个回答

13

由于您只需要结果中的一个对象,因此可以使用 array#reduce 来实现:

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);


7
使用array#mapObject#assign创建一个包含idtitle的对象。

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);

使用Object.fromEntries()array#map也可以创建对象。

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);


5

使用Array.reduce函数的方法如下:

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

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

只需迭代您的内容并将每个条目添加到映射中。

0

    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);


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接