1

I have to make new object of array key which is contain key label and key value,

const parsedItem = Object.assign(
    [],
    this.props.factories.map((item, key) => {
      key.value = item.ID;
      key.label = item.name;
    })
);

this is my fetched data

getFetchItem = [
  {name:"room 1",ID:1},
  {name:"room 2",ID:2},
]

this is what i expected, which is got from getFetchItem variable

const setParsedItem=[
  {label:"room 1", value:1},
  {label:"room 2", value:2},
];
Ario Setiawan
  • 33
  • 2
  • 6
  • 4
    The function you pass to `map()` needs to be `item => ({ label: item.name, value: item.ID })` (your `key` is the index, a number, and the curly braces are interpreted as wrapping a function body so you need to wrap them in parens) – ChrisG Jul 22 '19 at 23:17
  • 1
    The `Object.assign` is completely redundant too. You only need the `map()` – Phil Jul 22 '19 at 23:20
  • Solved by this code: const setParsedItem = Object.assign( [], getFetchItem.map(data => { return {label: data.name, value: data.ID}; }) ); – Ario Setiawan Jul 22 '19 at 23:28

0 Answers0