javascript - javascript - 获取基于另一个数组内容排序的数组

我如何排序allGames数组排序为 installedGames


const allGames = [


 { id: 2, name: 'game2' },


 { id: 1, name: 'game1' },


 { id: 4, name: 'game4' },


 { id: 3, name: 'game3' },


]


const installedGames = [


 { id: 2, name: 'game2' },


 { id: 3, name: 'game3' }


]



const sorted = arr1.sort((a, b) => {


 return a.id - b.id//this just sorts by id


});



基本上,我想得到这个顺序:


{ id: 2, name: 'game2' },


{ id: 3, name: 'game3' },


{ id: 1, name: 'game1' },


{ id: 4, name: 'game4' }



时间:


let allGames = [


 { id: 2, name: 'game2' },


 { id: 1, name: 'game1' },


 { id: 4, name: 'game4' },


 { id: 3, name: 'game3' },


]


let installedGames = [


 { id: 3, name: 'game3' },


 { id: 2, name: 'game2' },



]



//Step 1. sort both arrays



allGames = allGames.sort( (a,b) => a.id - b.id )


installedGames = installedGames.sort( (a,b) => a.id - b.id )



//Step 2. remove installedGames from allGames



const remainingGames = allGames.filter( game =>!installedGames.find(g => g.name === game.name) )



//step 3. concatenate both



const sortedGames = installedGames.concat(remainingGames)



console.log(sortedGames)

此外,我建议改变模型。 我建议只拥有一个 installed 属性,而不是拥有两个不同的数组,然后为你的游戏添加一个属性。 这会让你的更轻松:


const allGames = [


 { id: 2, name: 'game2', installed : true },


 { id: 1, name: 'game1', installed : false },


 { id: 4, name: 'game4', installed : false },


 { id: 3, name: 'game3', installed : true },


]



const sortedGames = allGames.sort((a, b) =>


 +b.installed - +a.installed || a.id - b.id


);



console.log(sortedGames)

你可以使用 sort 方法和 inside findIndex,然后检查索引是否为 -1 并按结果排序。


const allGames = [{ id: 2, name: 'game2' },{ id: 1, name: 'game1' },{ id: 4, name: 'game4' },{ id: 3, name: 'game3' },]


const installedGames = [{ id: 2, name: 'game2' },{ id: 3, name: 'game3' }]



allGames.sort((a, b) => {


 const ai = installedGames.findIndex(({id}) => id === a.id);


 const bi = installedGames.findIndex(({id}) => id === b.id)


 return (bi!= -1) - (ai!= -1) || ai - bi


});



console.log(allGames)

你还可以创建对象形式的游戏,并按该对象排序。


const allGames = [{ id: 2, name: 'game2' },{ id: 1, name: 'game1' },{ id: 4, name: 'game4' },{ id: 3, name: 'game3' },]


const installedGames = [{ id: 2, name: 'game2' },{ id: 3, name: 'game3' }]


const inst = installedGames.reduce((r, {id}, i) => Object.assign(r, {[id]: i}), {})



allGames.sort((a, b) => (b.id in inst) - (a.id in inst) || inst[a.id] - inst[b.id]);


console.log(allGames)

你可以尝试以下方法映射第二个数组并创建一个id数组。 现在根据映射数组的索引中的索引对第一个数组排序。


const allGames = [{ id: 2, name: 'game2' },{ id: 1, name: 'game1' },{ id: 4, name: 'game4' },{ id: 3, name: 'game3' }];


const installedGames = [{ id: 2, name: 'game2' },{ id: 3, name: 'game3' }];



const installedGameIds = installedGames.map(v => v.id);



allGames.sort((a,b) => {


 let aIndex = installedGameIds.indexOf(a.id);


 let bIndex = installedGameIds.indexOf(b.id);


 return aIndex> - 1? bIndex> -1? aIndex - bIndex : -1 : 1;


});


console.log(allGames);

这是我的尝试:


const allGames = [


 { id: 2, name: 'game2' },


 { id: 1, name: 'game1' },


 { id: 4, name: 'game4' },


 { id: 3, name: 'game3' },


]


const installedGames = [


 { id: 2, name: 'game2' },


 { id: 3, name: 'game3' }


];



var ids = installedGames.map(x => x.id);



const sorted = allGames.slice().sort((a, b) => {


 var pos_a = ids.indexOf(a.id);


 var pos_b = ids.indexOf(b.id);


//compare both installed


 if (pos_a!== -1 && pos_b!== -1) {


 return pos_a - pos_b;


 }


//compare installed and not installed


 if (pos_a!= -1) {


 return -1;


 }


//sort the rest


 return 1;


});


console.log(sorted);

你可以试试


const allGames = [


 { id: 2, name: 'game2' },


 { id: 1, name: 'game1' },


 { id: 4, name: 'game4' },


 { id: 3, name: 'game3' },


]


const installedGames = [


 { id: 2, name: 'game2' },


 { id: 3, name: 'game3' }


]



const sorted = ((a, b) => {


 return a.id - b.id//this just sorts by id


});


var out = [...installedGames.sort(sorted),.. .allGames.filter(o=>!(installedGames.map(i => i.id).indexOf(o.id)> -1)).sort(sorted)]



console.log(out)



...