亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

比較兩個對象數組,按array1的順序保留array2中的項目

比較兩個對象數組,按array1的順序保留array2中的項目

慕娘9325324 2022-10-21 10:15:40
我有 2 個對象數組:const headers1 = [    {text: 'ID', value: 'id', active: true},    {text: 'Name', value: 'name', active: true},    {text: 'Age', value: 'age', active: false},    {text: 'Address', value: 'address', active: true},    {text: 'Phone', value: 'phone', active: true}, //should be excluded because are not on headers2]和const headers2 = [    {text: 'Name',value:'name', active: true},    {text: 'Age',value:'age', active: true},     {text: 'Address',value:'address', active: true},    {text: 'ID',value: 'id', active: true},                   {text: 'Config',value: 'config', active: false}, //should be included at the end    {text: 'Options',value: 'options',active: true} //sould be included at the end]所以基本上我需要headers2從headers1. headers2未打開的項目headers1應放在最后。結果應該是這樣的:const headers3 = [    {text: 'ID', value: 'id', active: true},    {text: 'Name', value: 'name', active: true},    {text: 'Age', value: 'age', active: false},    {text: 'Address', value: 'address', active: true},    {text: 'Config', value: 'config', active: false},    {text: 'Options', value: 'options', active: true}]
查看完整描述

3 回答

?
躍然一笑

TA貢獻1826條經驗 獲得超6個贊

只需按另一項中匹配項的索引進行排序,如果-1將索引設置為Infinity:


const headers1 = [

    {text: 'ID', value: 'id', active: true},

    {text: 'Name', value: 'name', active: true},

    {text: 'Age', value: 'age', active: false},

    {text: 'Address', value: 'address', active: true},

    {text: 'Phone', value: 'phone', active: true} //should be excluded because are not on headers2

]


const headers2 = [

    {text: 'Name', value: 'name', active: true},

    {text: 'Age', value: 'age', active: true}, 

    {text: 'Address', value: 'address', active: true},

    {text: 'ID', value: 'id', active: true},               

    {text: 'Config', value: 'config', active: false}, //should be included at the end

    {text: 'Options', value: 'options', active: true} //sould be included at the end

]


const headers3 = [...headers2].sort((a, b) => {

    const aIndex = headers1.findIndex((i) => i.text === a.text) + 1 || Infinity;

    const bIndex = headers1.findIndex((i) => i.text === b.text) + 1 || Infinity;

    return aIndex - bIndex;

}).map((i) => {

    i.active = (headers1.find((el) => el.text === i.text) || i).active

    return i;

});


console.log(headers3);


查看完整回答
反對 回復 2022-10-21
?
慕沐林林

TA貢獻2016條經驗 獲得超9個贊

您可以使用想要的順序構建一個對象,并從第二個數組中獲取一個副本并對其進行排序。


const headers1 = [{ text: 'ID', value: 'id', active: true }, { text: 'Name', value: 'name', active: true }, { text: 'Age', value: 'age', active: false }, { text: 'Address', value: 'address', active: true }, { text: 'Phone', value: 'phone', active: true }],

    headers2 = [{ text: 'Name', value: 'name', active: true }, { text: 'Age', value: 'age', active: true }, { text: 'Address', value: 'address', active: true }, { text: 'ID', value: 'id', active: true }, { text: 'Config', value: 'config', active: false }, { text: 'Options', value: 'options', active: true }],

    references = headers1.reduce((r, o, i) => (r[o.text] = { o, order: i + 1 }, r), {}),

    result = headers2

        .map(o => references[o.text]?.o || o)

        .sort(({ text: a }, { text: b }) => (references[a]?.order || Number.MAX_VALUE) - (references[b]?.order || Number.MAX_VALUE));


console.log(result);

.as-console-wrapper { max-height: 100% !important; top: 0; }


查看完整回答
反對 回復 2022-10-21
?
慕妹3146593

TA貢獻1820條經驗 獲得超9個贊

您可以Map在 中的項目上使用 a headers2,由 鍵入name。然后從該地圖中收集項目headers1,最后將地圖中不匹配的項目添加到該結果中。您可以使用deletemap 的方法,因為它還返回是否找到鍵。


就兩個數組中的項目數而言,這具有線性時間復雜度。


以下是它的工作原理:


const headers1 = [{text: 'ID', value: 'id', active: true},{text: 'Name', value: 'name', active: true},{text: 'Age', value: 'age', active: false},{text: 'Address', value: 'address', active: true},{text: 'Phone', value: 'phone', active: true}]

const headers2 = [{text: 'Name',value:'name', active: true},{text: 'Age',value:'age', active: true}, {text: 'Address',value:'address', active: true},{text: 'ID',value: 'id', active: true},{text: 'Config',value: 'config', active: false},{text: 'Options',value: 'options',active: true}];


// Collect headers2 items in a map keyed by name

const map = new Map(headers2.map(o => [o.value, o]));

// First get the items from headers1 that are in the map, delete them from that map,

// and then add the remaining map values

let headers3 = [...headers1.filter(o => map.delete(o.value)), ...map.values()];


// output result

headers3.forEach(o => console.log(JSON.stringify(o)));


查看完整回答
反對 回復 2022-10-21
  • 3 回答
  • 0 關注
  • 148 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號