簡體   English   中英

合並兩個數組,每個數組包含一些字段

[英]merge two arrays including some fields from each

我想使用map函數合並兩個數組,這個想法是創建一個新數組,其中一些字段取自第一個數組,某些字段取自第二個數組。 條件是字段名稱

這是清單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"}
]

結果,它應該返回合並的版本,但是只包括一些選定的字段:

[
    {"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"}
]

這是我嘗試使用map合並這兩個數組的方法,但不是很成功。 有人可以幫助我實現這一目標嗎?

const alldata = listA.map(u => listB.filter(oo => u.name === oo.name));

我用name為重點,以找到一個條目是否a存在於b並創造了從兩個列表中相應的值所需要的清單。

 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) 

有了這個答案,您不會創建新的數組。

 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); 

你很親密 嘗試這個:

 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 ); 

使用map創建新數組,並filter以從第二個元素中找到匹配的元素

 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) 

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 };
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM