I want to merge two arrays using map function, the idea is to create a new array with some fields taken from first array and some fields from the second one. The condition is the field name.
Here is list A:
[
{"name": "tom", "id": "1", "date": "1654"},
{"name": "jack", "id": "2", "date": "6544"},
{"name": "sarah", "id": "3", "date": "987"},
{"name": "william", "id": "4", "date": "654"},
{"name": "ronaldo", "id": "5", "date": "12345"}
]
and here is list B:
[
{"name": "tom", "age": "20", "school": "A", "password": "abcd"},
{"name": "jack", "age": "25", "school": "B", "password": "1234"}
]
as a result, it should return a merged version, but only some selected fields included:
[
{"name": "tom", "age": "20", "school": "A", "exists": true, , "date": "1654"},
{"name": "jack", "age": "25", "school": "B", "exists": true, "date": "6544"},
{"name": "sarah", "age": "", "school": "", "exists": false, "date": "987"},
{"name": "william", "age": "", "school": "", "exists": false, "date": "654"},
{"name": "ronaldo", "age": "", "school": "", "exists": false, "date": "12345"}
]
Here is my attempt to merge these two arrays, using map, but not very successful. Could somebody help me to achieve this?
const alldata = listA.map(u => listB.filter(oo => u.name === oo.name));