-1

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));
Bonnard
  • 389
  • 2
  • 8
  • 26
  • You're close. You just need to return a u with true or false set in the mapping function based on whether filter is true or false. – Robert Moskal Feb 18 '18 at 15:33

5 Answers5

1

I used name as the key to find whether an entry from a is present in b and created the required list with appropriate values from both the lists.

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"}
];
b=[
    {"name": "tom", "age": "20", "school": "A", "password": "abcd"},
    {"name": "jack", "age": "25", "school": "B", "password": "1234"}
];

c = a.map(a1 => {
      let b3 = b.find(b1=>b1.name === a1.name) || {};
      return {name: a1.name, age: b3.age || "", school: b3.school || "", exists: b3.name != undefined, date: a1.date}
   })
console.log(c)
curlyBraces
  • 1,095
  • 8
  • 12
1

with this answer you're not creating new arrays.

let 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"}],
    b = [{"name": "tom","age": "20","school": "A","password": "abcd"},{"name": "jack","age": "25","school": "B","password": "1234"}];
a.forEach((key, index)=>{
  let exists;
  b.forEach((bkey, bindex)=>{
    if(key.name==bkey.name){
      a[index] = {...a[index], ...b[bindex]};
      exists = bkey.age;
    }
  });
  a[index].exists = !!exists;
});
console.log(a);
alessandrio
  • 4,282
  • 2
  • 29
  • 40
0

You are very close. Try this:

let arrA = [
    {"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"}
];
 
let arrB = [
    {"name": "tom", "age": "20", "school": "A", "password": "abcd"},
    {"name": "jack", "age": "25", "school": "B", "password": "1234"}
];
 
let result = arrA.map(v => {
    let fr = arrB.filter(f => v.name == f.name ) ;
    let e = fr.length ? true : false;
    fr = fr.length ? fr[0] : {age: "", school: "", password: "" };
 
    //Construct the format you want on the final array
    return  {
        name: v.name, 
        age: fr.age, 
        school: fr.school, 
        exists: e, 
        date: v.date
    }
});
 
console.log( result );
Eddie
  • 26,593
  • 6
  • 36
  • 58
0

Use map to create new array and filter to find the matched element from the second element

var 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"
  }
]
var b = [{
    "name": "tom",
    "age": "20",
    "school": "A",
    "password": "abcd"
  },
  {
    "name": "jack",
    "age": "25",
    "school": "B",
    "password": "1234"
  }
]

var c = a.map(function(item) {
  let d = b.filter(function(items) {
    return item.name === items.name;
  })
  if (d.length > 0) {
    item.age = d[0].age;
    item.school = d[0].school;
    item.password = d[0].password
  }
  return item;
})
console.log(c)
brk
  • 48,835
  • 10
  • 56
  • 78
0
const list1 = [
    {"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"}
];

const list2 = [
    {"name": "tom", "age": "20", "school": "A", "password": "abcd"},
    {"name": "jack", "age": "25", "school": "B", "password": "1234"}
];

const merged = list1.map(item => {
    const list2Match = list2.filter(i => i.name === item.name)[0];
    const obj = list2Match ? {school: list2Match.school, age: list2Match.age, exists: true} : { school: '', age: '', exists: false};

    return {school:obj.school, age: obj.age, exists: obj.exists, name: item.name, date: item.date };
});
Amir Popovich
  • 29,350
  • 9
  • 53
  • 99