簡體   English   中英

如何使用javascript以以下格式合並兩個對象數組

[英]how to merge two array of object in following format using javascript

array1 = [{
    questionId: "5e52b55330bb2cee102b9a39",
    note: "Name",
    prefillValue: "prasanna"
},

{
    questionId: "5e52b56b30bb2cee102b9a3f",
    note: "Mobile Number",
    prefillValue: null
},
{
    questionId: "5e52b58230bb2cee102b9a42",
    note: "Agent Email",
    prefillValue: null
},
{
    questionId: "5e52b55e30bb2cee102b9a3c",
    note: "Email",
    prefillValue: null
},
{
    questionId: "5e52c39730bb2cee102b9a47",
    note: "Agent ID",
    prefillValue: null
},
{
    questionId: "5e54dbdd30bb2c6018f488ca",
    note: "Location",
    prefillValue: null
}
]


array2 = [{
    questionId: "5e52b55e30bb2cee102b9a3c",
    note: "Email",
    prefillValue: "skk@gmail.com"
},
{
    questionId: "5e52c39730bb2cee102b9a47",
    note: "Agent ID",
    prefillValue: "34"
},
{
    questionId: "5e54dbdd30bb2c6018f488ca",
    note: "Location",
    prefillValue: "Chennai"
}
]

預期輸出:

array3 = [{
        questionId: "5e52b55330bb2cee102b9a39",
        note: "Name",
        prefillValue: "prasanna"
    },

    {
        questionId: "5e52b56b30bb2cee102b9a3f",
        note: "Mobile Number",
        prefillValue: null
    }, {
        questionId: "5e52b58230bb2cee102b9a42",
        note: "Agent Email",
        prefillValue: null
    }, {
        questionId: "5e52b55e30bb2cee102b9a3c",
        note: "Email",
        prefillValue: null
    }, {
        questionId: "5e52b55e30bb2cee102b9a3c",
        note: "Email",
        prefillValue: "skk@gmail.com"
    }, {
        questionId: "5e52c39730bb2cee102b9a47",
        note: "Agent ID",
        prefillValue: "34"
    },
    {
        questionId: "5e54dbdd30bb2c6018f488ca",
        note: "Location",
        prefillValue: "Chennai"
    }
]

 let array1 = [{ questionId: "5e52b55330bb2cee102b9a39", note: "Name", prefillValue: "prasanna" }, { questionId: "5e52b56b30bb2cee102b9a3f", note: "Mobile Number", prefillValue: null }, { questionId: "5e52b58230bb2cee102b9a42", note: "Agent Email", prefillValue: null }, { questionId: "5e52b55e30bb2cee102b9a3c", note: "Email", prefillValue: null }, { questionId: "5e52c39730bb2cee102b9a47", note: "Agent ID", prefillValue: null }, { questionId: "5e54dbdd30bb2c6018f488ca", note: "Location", prefillValue: null } ] let array2 = [{ questionId: "5e52b55e30bb2cee102b9a3c", note: "Email", prefillValue: "skk@gmail.com" }, { questionId: "5e52c39730bb2cee102b9a47", note: "Agent ID", prefillValue: "34" }, { questionId: "5e54dbdd30bb2c6018f488ca", note: "Location", prefillValue: "Chennai" } ] let array3 = [...array1,...array2]; console.log(array3)

您可以使用Object.assign將屬性從一個對象復制到另一個對象

 const array1 = [{ questionId: "5e52b55330bb2cee102b9a39", note: "Name", prefillValue: "prasanna" }, { questionId: "5e52b56b30bb2cee102b9a3f", note: "Mobile Number", prefillValue: null }, { questionId: "5e52b58230bb2cee102b9a42", note: "Agent Email", prefillValue: null }, { questionId: "5e52b55e30bb2cee102b9a3c", note: "Email", prefillValue: null }, { questionId: "5e52c39730bb2cee102b9a47", note: "Agent ID", prefillValue: null }, { questionId: "5e54dbdd30bb2c6018f488ca", note: "Location", prefillValue: null } ] const array2 = [{ questionId: "5e52b55e30bb2cee102b9a3c", note: "Email", prefillValue: "skk@gmail.com" }, { questionId: "5e52c39730bb2cee102b9a47", note: "Agent ID", prefillValue: "34" }, { questionId: "5e54dbdd30bb2c6018f488ca", note: "Location", prefillValue: "Chennai" } ] const array3 = array1.map(o => Object.assign(o, array2.find(a => a.questionId === o.questionId))); console.log(array3)

filter() 方法創建一個新數組,其中包含通過所提供函數實現的測試的所有元素。

您可以通過這種方式刪除重復的鍵。

let array1 = [{
    questionId: "5e52b55330bb2cee102b9a39",
    note: "Name",
    prefillValue: "prasanna"
},

{
    questionId: "5e52b56b30bb2cee102b9a3f",
    note: "Mobile Number",
    prefillValue: null
},
{
    questionId: "5e52b58230bb2cee102b9a42",
    note: "Agent Email",
    prefillValue: null
},
{
    questionId: "5e52b55e30bb2cee102b9a3c",
    note: "Email",
    prefillValue: null
},
{
    questionId: "5e52c39730bb2cee102b9a47",
    note: "Agent ID",
    prefillValue: null
},
{
    questionId: "5e54dbdd30bb2c6018f488ca",
    note: "Location",
    prefillValue: null
}
]

var uniq = {}
var arrFiltered = arrays1.filter(obj => !uniq[obj.questionId] && (uniq[obj.questionId] = true));
console.log('Filtered Array:', arrFiltered)

參考可以在這里找到

 let array1 = [{ questionId: "5e52b55330bb2cee102b9a39", note: "Name", prefillValue: "prasanna" }, { questionId: "5e52b56b30bb2cee102b9a3f", note: "Mobile Number", prefillValue: null }, { questionId: "5e52b58230bb2cee102b9a42", note: "Agent Email", prefillValue: null }, { questionId: "5e52b55e30bb2cee102b9a3c", note: "Email", prefillValue: null }, { questionId: "5e52c39730bb2cee102b9a47", note: "Agent ID", prefillValue: null }, { questionId: "5e54dbdd30bb2c6018f488ca", note: "Location", prefillValue: null } ] let array2 = [{ questionId: "5e52b55e30bb2cee102b9a3c", note: "Email", prefillValue: "skk@gmail.com" }, { questionId: "5e52c39730bb2cee102b9a47", note: "Agent ID", prefillValue: "34" }, { questionId: "5e54dbdd30bb2c6018f488ca", note: "Location", prefillValue: "Chennai" } ] let array3 = array1.concat(...array2); console.log(array3)

暫無
暫無

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

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