0

I am able to achieve my goal, I split list to be [0] and [1], I am thinking maybe there is a better solution with es6? Thanks

const list = [
  [
      { value: 'Apple' },
      { value: 'Banana'},
      { value: 'Orange'},
      { value: 'Grape' },
  ],
  [
      {value: 'color is Red' },
      {value: 'color is Yellow'},
      {value: 'color is Orange'},
      {value: 'color is green'},
  ]]


const a = list[0];
const b = list[1];


const c = a.map(({value}, index)=>{
  return {[value] : b[index].value};
})

console.log(c)

result to expect is

[
  {
    "Apple": "color is Red"
  },
  {
    "Banana": "color is Yellow"
  },
  {
    "Orange": "color is Orange"
  },
  {
    "Grape": "color is green"
  }
]
olo
  • 5,225
  • 15
  • 52
  • 92
  • 3
    `const [a, b] = list;` but I'm not sure it's THAT much better. Unless you mean the next operation, in which case check [Javascript equivalent of Python's zip function](https://stackoverflow.com/q/4856717) – VLAZ Apr 19 '21 at 11:05
  • Can you change the input format? It's weird to have a list of exactly two elements. – Bergi Apr 19 '21 at 11:06
  • Thanks, no, I can't change the input. – olo Apr 19 '21 at 11:08

2 Answers2

2

You can do it with Array destructuring.

const [first, second] = list;
first.map(({value}, index) => {
  return {[value] : second[index].value};
});
bring2dip
  • 886
  • 2
  • 11
  • 22
0

const list = [
  [
    { value: "Apple" },
    { value: "Banana" },
    { value: "Orange" },
    { value: "Grape" },
  ],
  [
    { value: "color is Red" },
    { value: "color is Yellow" },
    { value: "color is Orange" },
    { value: "color is green" },
  ],
];

const [fruit, prop] = list;
const result = fruit.map(({ value }, i) => ({ [value]: prop[i].value }));
console.log(result);
DecPK
  • 24,537
  • 6
  • 26
  • 42