how to use filter and map to alter object

Viewed 40

how to modify the existing object by using map and filter.. below I have given two objects and needed output.

let a = [
  { a: "hi", b: 0 },
  { a: "bye", b: 1 },
  { a: "seeyou", b: 2 },
];
let b = { hi: "22:00", bye: "20:00", seeyou: "12:00" };

here I need like this response as below

c = [
  { a: "hi", b: 0, time: "22:00" },
  { a: "bye", b: 1, time: "20:00" },
  { a: "seeyou", b: 2, time: "12:00" },
];

how modify the object a to c using map and filter

1 Answers

Do you just want to get an expected result?

If so, example below

let a = [
  { a: "hi", b: 0 },
  { a: "bye", b: 1 },
  { a: "seeyou", b: 2 },
];
let b = { hi: "22:00", bye: "20:00", seeyou: "12:00" };

const output = a.map(el => ({
  ...el,
  time: b[el.a],
}));

console.log(output);

Related