最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Mapping a JSON const in React.js - Stack Overflow

programmeradmin5浏览0评论

I have a bunch of configurations in a const in my React.js app and the JSON looks like this:

const original = [
  { 'id': '1', 'desc': 'AAA', 'isOK': true },
  { 'id': '2', 'desc': 'BBB', 'isOK': true },
  { 'id': '3', 'desc': 'CCC', 'isOK': false },
  ...
];

It contains a bunch of configurations that I'll likely put in a separate file somewhere, but for now I wanted to test it and thought that a const would be appropriate.

I'm attempting to use the ReactiveSearch ponent and feed this to MultiDataList's data field for which it needs to be transformed into:

[
  { label: '1', value: 'AAA' },
  { label: '2', value: 'BBB' },
  { value: '3', value: 'CCC' },
  ...
]

I had assumed that original.map(e => { 'label': e.id, 'value': e.desc }) would do the trick but I get that 'Unexpected token' at the first colon. IntelliJ says 'Expression statement is not assignment or call'.

Mapping through the array with individual items works just fine, e.g. original.map(e => e.id), so I am not quite sure what I'm doing wrong. I've only started tinkering with JavaScript and React.js.

I have a bunch of configurations in a const in my React.js app and the JSON looks like this:

const original = [
  { 'id': '1', 'desc': 'AAA', 'isOK': true },
  { 'id': '2', 'desc': 'BBB', 'isOK': true },
  { 'id': '3', 'desc': 'CCC', 'isOK': false },
  ...
];

It contains a bunch of configurations that I'll likely put in a separate file somewhere, but for now I wanted to test it and thought that a const would be appropriate.

I'm attempting to use the ReactiveSearch ponent and feed this to MultiDataList's data field for which it needs to be transformed into:

[
  { label: '1', value: 'AAA' },
  { label: '2', value: 'BBB' },
  { value: '3', value: 'CCC' },
  ...
]

I had assumed that original.map(e => { 'label': e.id, 'value': e.desc }) would do the trick but I get that 'Unexpected token' at the first colon. IntelliJ says 'Expression statement is not assignment or call'.

Mapping through the array with individual items works just fine, e.g. original.map(e => e.id), so I am not quite sure what I'm doing wrong. I've only started tinkering with JavaScript and React.js.

Share Improve this question edited Oct 26, 2018 at 18:44 Ashley Mills 53.2k17 gold badges137 silver badges172 bronze badges asked Oct 24, 2018 at 6:05 Max PowerMax Power 9921 gold badge10 silver badges26 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

In order to implicitly return an object from an arrow function, you need to surround the { and } brackets in parentheses, else the interpreter doesn't know how to differentiate it from an ordinary function block that starts with {:

const original = [
  { 'id': '1', 'desc': 'AAA', 'isOK': true },
  { 'id': '2', 'desc': 'BBB', 'isOK': true },
  { 'id': '3', 'desc': 'CCC', 'isOK': false },
];

console.log(
  original.map(e => ({ label: e.id, value: e.desc }))
);

Note that there's no need to put quotes around the object properties (which are assumed to be strings by default) - only string values need to be enclosed in string delimiters.

Another method, using destructuring:

const original = [
  { 'id': '1', 'desc': 'AAA', 'isOK': true },
  { 'id': '2', 'desc': 'BBB', 'isOK': true },
  { 'id': '3', 'desc': 'CCC', 'isOK': false },
];

console.log(
  original.map(({ id, desc }) => ({ label: id, value: desc }))
  // or:
  // original.map(({ id: label, desc: value }) => ({ label, value }))
);

Yes you got it slightly wrong.

const original = [
 { 'id': '1', 'desc': 'AAA', 'isOK': true },
 { 'id': '2', 'desc': 'BBB', 'isOK': true },
 { 'id': '3', 'desc': 'CCC', 'isOK': false },
 ...
];

In the ES5 syntax it look's like this:

original.map(config=>({label:config.id,value:config.desc}))

Also notice the paranthesis around the returning object.It is necessary.

You can also do it like this.

original.map(function(config){
   return {
      label:config.id,
      value:config.desc
   }
})
发布评论

评论列表(0)

  1. 暂无评论