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

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

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)));
添加回答
舉報