IT

ES6는 배열을 매핑하여 새 키가있는 배열을 반환합니다.

lottoking 2020. 8. 14. 08:02
반응형

ES6는 배열을 매핑하여 새 키가있는 배열을 반환합니다. [중복]


이 질문에 이미 답변이 있습니다.

개체 배열이 있습니다.

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

반환 할 간단한 한 줄을 찾고 :

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

따라서 적절한 키를 사용하여 쉽게 반응 드롭 다운으로 펌핑 할 수 있습니다.

이 간단한 솔루션이 작동 할 것 같지만 잘못된 구문 오류가 발생합니다.

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

개체를 사용하면 만하면됩니다. ()

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

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

참고 URL : https://stackoverflow.com/questions/40348171/es6-map-an-array-of-objects-to-return-an-array-of-objects-with-new-keys

반응형