ES6 映射对象数组,以返回具有新键的对象数组 [英] ES6 map an array of objects, to return an array of objects with new keys

查看:32
本文介绍了ES6 映射对象数组,以返回具有新键的对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象数组:

<预><代码>[{编号:1,名称:'账单'},{编号:2,名称:'泰'}]

寻找一个简单的单线返回:

<预><代码>[{值:1,文字:'账单'},{值:2,文字:'泰'}]

所以我可以使用正确的键轻松地将它们泵入反应下拉列表.

我觉得这个简单的解决方案应该可行,但我收到无效的语法错误:

this.props.people.map(person => { value: person.id, text: person.name })

解决方案

你只需要在()

中包裹对象

var arr = [{编号:1,名称:'账单'}, {编号:2,名称:'泰'}]var 结果 = arr.map(person => ({ value: person.id, text: person.name }));console.log(result)

I have an array of objects:

[
    {
        id: 1,
        name: 'bill'
    },
    {
        id: 2,
        name: 'ted'
    }
]

Looking for a simple one-liner to return:

[
    {
        value: 1,
        text: 'bill'
    },
    {
        value: 2,
        text: 'ted'
    }
]

So I can easily pump them into a react dropdown with the proper keys.

I feel like this simple solution should work, but I'm getting invalid syntax errors:

this.props.people.map(person => { value: person.id, text: person.name })

解决方案

You just need to wrap object in ()

var arr = [{
  id: 1,
  name: 'bill'
}, {
  id: 2,
  name: 'ted'
}]

var result = arr.map(person => ({ value: person.id, text: person.name }));
console.log(result)

这篇关于ES6 映射对象数组,以返回具有新键的对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆