我发现很难用标题来描述我遇到的问题,但是我希望举个例子会有所帮助。我有一个对象的javascript数组,看起来像这样:
const myPlayer = "jim"
const myArray =
[
{"player":"bob", "points":11},
{"player":"joe", "points":15},
{"player":"nik", "points":18},
{"player":"tom", "points":21},
{"player":"jim", "points":25},
{"player":"ron", "points":26},
{"player":"tim", "points":32},
{"player":"jon", "points":35},
{"player":"len", "points":42},
{"player":"eva", "points":51}
];
我的数组按键要点对对象进行排序,我想过滤该数组以保留(a) player 键与 myPlayer匹配的对象变量,以及(b)与 myPlayer 变量匹配的对象之前的2个对象和之后的2个对象。例如,在这种情况下,我想返回以下内容:
const filtArray =
[
{"player":"nik", "points":18},
{"player":"tom", "points":21},
{"player":"jim", "points":25},
{"player":"ron", "points":26},
{"player":"tim", "points":32}
];
...因为nik和tom是jim之前的两个对象,而ron和tim是tim之后的两个对象。 player 键的每个对象都有唯一的值,因此永远不会有两个具有相同 player 键的对象。
这个问题的最后一部分-我希望过滤后的数组始终包含5个对象。因此,如果myPlayer = "bob"
(其中bob是第一个对象中的 player ),我希望它简单地返回前5个对象,如下所示:
[
{"player":"bob", "points":11},
{"player":"joe", "points":15},
{"player":"nik", "points":18},
{"player":"tom", "points":21},
{"player":"jim", "points":25}
];
...如果myPlayer =“ joe”,或者myPlayer =“ len”或“ eva”,则输出类似。
对此表示感谢,谢谢!!
编辑:看起来我只是在代码中有一个小错误,这破坏了我的方法...具有以下内容,它使用findIndex和slice:
const index = myArray.findIndex(x => x.player === myPlayer);
filtArray = myArray.slice(index-2, index+2)
答案 0 :(得分:0)
您可以为第一部分做一个for loop
,然后为第二部分做slice()
,但是正如您所说的,如果选择eva
会显示前5个,如果您想要其他内容,可以改变切片。
const myPlayer = "ron";
const myArray = [
{ player: "bob", points: 11 },
{ player: "joe", points: 15 },
{ player: "nik", points: 18 },
{ player: "tom", points: 21 },
{ player: "jim", points: 25 },
{ player: "ron", points: 26 },
{ player: "tim", points: 32 },
{ player: "jon", points: 35 },
{ player: "len", points: 42 },
{ player: "eva", points: 51 }
];
var newArray = [];
for (let i = 0; i < myArray.length; i++) {
if (myArray[i].player == myPlayer){
var index = i;//<-- store the index to probably be used in slice
if ((i-2)>=0 && (i+2)<myArray.length) {
newArray.push(myArray[i - 2]);
newArray.push(myArray[i - 1]);
newArray.push(myArray[i]);
newArray.push(myArray[i + 1]);
newArray.push(myArray[i + 2]);
}
}
}
if(newArray.length == 0){
newArray = myArray.slice(0, 6)
}
console.log(newArray);
答案 1 :(得分:0)
您可以通过找到要寻找的球员的索引,然后遍历数组并创建索引在该范围内的新球员列表来实现。
const myPlayer = "jim"
const myArray = [
{"player":"bob", "points":11},
{"player":"joe", "points":15},
{"player":"nik", "points":18},
{"player":"tom", "points":21},
{"player":"jim", "points":25},
{"player":"ron", "points":26},
{"player":"tim", "points":32},
{"player":"jon", "points":35},
{"player":"len", "points":42},
{"player":"eva", "points":51}
]
function indexOfPlayer(arr, player) {
return arr.map(i => i.player).indexOf(player)
}
function getItemWithSurrounding(arr, index, around) {
result = []
for (i = index - around; i <= index + around; i++) {
if (i >= 0 && i < arr.length) {
result.push(arr[i])
}
}
return result
}
playerIndex = indexOfPlayer(myArray, myPlayer)
players = getItemWithSurrounding(myArray, playerIndex, 2)
console.log(players)
输出:
[
{
"player": "nik",
"points": 18
},
{
"player": "tom",
"points": 21
},
{
"player": "jim",
"points": 25
},
{
"player": "ron",
"points": 26
},
{
"player": "tim",
"points": 32
}
]
答案 2 :(得分:0)
您可以使用Array.findIndex
方法在数组中找到正确的播放器,然后将Array.slice
与两个if语句一起使用以选择数组的正确段(如果是选择第一名或第二名,如果选择了最后两名,则选择最后五名,或者选择“内部”玩家中的五名。
const myPlayer = "jim"
const myArray =
[
{"player":"bob", "points":11},
{"player":"joe", "points":15},
{"player":"nik", "points":18},
{"player":"tom", "points":21},
{"player":"jim", "points":25},
{"player":"ron", "points":26},
{"player":"tim", "points":32},
{"player":"jon", "points":35},
{"player":"len", "points":42},
{"player":"eva", "points":51}
];
function filterArray(array, playerName) {
let idx = array.findIndex(o => o.player === playerName)
if (array.length - idx <= 2) {
return array.slice(array.length - 5)
} else if (idx < 2) {
return array.slice(0, 5)
} else {
return array.slice(idx - 2, idx + 3)
}
}
console.log(filterArray(myArray, myPlayer)) // "jim" is an "inner" player, so the surrounding elements are returned
console.log(filterArray(myArray, "joe")) // "joe" is an "outer" player, so the first five elements are returned
console.log(filterArray(myArray, "eva")) // "eva" is an "outer" player, so the last five elements are returned