如何使用 JS 从这个 JSON 数组中获取重复的对象?
How can I get the duplicate objects from this JSON array with JS?
从下面 JSON 我想获取球员并按他们被引用的次数列出他们。因此,例如,球员 3,切尔西被引用一次,是排名第一的顶级球员,然后吉姆被引用两次将是排名第二,然后球员一,乔,将是列表中的最后一个,因为他们被引用了 3 次.所以从最少到最多。
我正在考虑通过 JS 使用数组过滤方法来实现,但不确定是否可行?有没有办法让我使用过滤器将数组中的一个对象与另一个对象进行比较?
我在想类似下面这个 JS 的东西,for 循环的第一部分,会将每个玩家与数组中的下一个玩家进行比较,但它不会准确地比较它们,因为它会过滤每个循环,所以我image 会破坏性能并且过滤不准确。这就是为什么我还包括了 if 条件 我不知道这是否比过滤器更好?
let topPlayers = []
for(let i=0; i<players.length; i++) {
players.filter((player) => player[i] == player[i + 1])
if(players[i].title == players[i+1].title) {
topPlayers.push(players[i])
}
}
const players = [
{
"header": "Player",
"title": "Player one",
"subtitles": [
{
"name": "Joe"
}
],
"time": "2016-11-08T16:03:08.957Z",
"products": [
"Xbox"
]
},
{
"header": "Player",
"title": "Player two",
"subtitles": [
{
"name": "Jim"
}
],
"time": "2016-11-08T16:03:08.957Z",
"products": [
"Xbox"
]
},
{
"header": "Player",
"title": "Player one",
"subtitles": [
{
"name": "Joe"
}
],
"time": "2016-11-08T16:03:08.957Z",
"products": [
"Xbox"
]
},
{
"header": "Player",
"title": "Player two",
"subtitles": [
{
"name": "Jim"
}
],
"time": "2016-11-08T16:03:08.957Z",
"products": [
"Xbox"
]
},
{
"header": "Player",
"title": "Player three",
"subtitles": [
{
"name": "Chelsea"
}
],
"time": "2016-11-08T16:03:08.957Z",
"products": [
"PC"
]
},
{
"header": "Player",
"title": "Player one",
"subtitles": [
{
"name": "Joe"
}
],
"time": "2016-11-08T16:03:08.957Z",
"products": [
"Xbox"
]
}
]
您可以使用另一个对象为每个唯一的玩家头衔保存一个 属性,然后每次遇到相同的头衔时增加此 属性 的值:
const counter = {};
for(const player in players) {
// checks whether property named as player already exists
if(!counter[player.title]) {
// Create new property, named as player, with 1 as value
counter[player.title] = 1;
} else {
// Increment property value
counter[player.title]++;
}
}
for(const player in counter) {
console.log(`The player ${player} is recorded ${counter[counter]} times`);
}
一种方法是结合使用 .reduce(..)
and .sort(..)
.
这是一个例子:
const players = [
{
"header": "Player",
"title": "Player one",
"subtitles": [
{
"name": "Joe"
}
],
"time": "2016-11-08T16:03:08.957Z",
"products": [
"Xbox"
]
},
{
"header": "Player",
"title": "Player two",
"subtitles": [
{
"name": "Jim"
}
],
"time": "2016-11-08T16:03:08.957Z",
"products": [
"Xbox"
]
},
{
"header": "Player",
"title": "Player one",
"subtitles": [
{
"name": "Joe"
}
],
"time": "2016-11-08T16:03:08.957Z",
"products": [
"Xbox"
]
},
{
"header": "Player",
"title": "Player two",
"subtitles": [
{
"name": "Jim"
}
],
"time": "2016-11-08T16:03:08.957Z",
"products": [
"Xbox"
]
},
{
"header": "Player",
"title": "Player three",
"subtitles": [
{
"name": "Chelsea"
}
],
"time": "2016-11-08T16:03:08.957Z",
"products": [
"PC"
]
},
{
"header": "Player",
"title": "Player one",
"subtitles": [
{
"name": "Joe"
}
],
"time": "2016-11-08T16:03:08.957Z",
"products": [
"Xbox"
]
}
]
const result = players.reduce((a, c) => {
const found = a.find((o) => o.title === c.title);
if (found) {
found.count++;
return a;
}
a.push({ title: c.title, subtitles: c.subtitles, count: 1 });
return a;
}, []).sort((a, b) => a.count - b.count);
console.log(result.map((o, i) => `Player: ${o.subtitles[0].name} is #${i+1}`));
这样做是首先将数组缩减为仅包含唯一条目(原始对象的简化版本)和每个对象出现次数的数组(count
属性).然后,它根据这个数字对数组进行排序。
从下面 JSON 我想获取球员并按他们被引用的次数列出他们。因此,例如,球员 3,切尔西被引用一次,是排名第一的顶级球员,然后吉姆被引用两次将是排名第二,然后球员一,乔,将是列表中的最后一个,因为他们被引用了 3 次.所以从最少到最多。
我正在考虑通过 JS 使用数组过滤方法来实现,但不确定是否可行?有没有办法让我使用过滤器将数组中的一个对象与另一个对象进行比较?
我在想类似下面这个 JS 的东西,for 循环的第一部分,会将每个玩家与数组中的下一个玩家进行比较,但它不会准确地比较它们,因为它会过滤每个循环,所以我image 会破坏性能并且过滤不准确。这就是为什么我还包括了 if 条件 我不知道这是否比过滤器更好?
let topPlayers = []
for(let i=0; i<players.length; i++) {
players.filter((player) => player[i] == player[i + 1])
if(players[i].title == players[i+1].title) {
topPlayers.push(players[i])
}
}
const players = [
{
"header": "Player",
"title": "Player one",
"subtitles": [
{
"name": "Joe"
}
],
"time": "2016-11-08T16:03:08.957Z",
"products": [
"Xbox"
]
},
{
"header": "Player",
"title": "Player two",
"subtitles": [
{
"name": "Jim"
}
],
"time": "2016-11-08T16:03:08.957Z",
"products": [
"Xbox"
]
},
{
"header": "Player",
"title": "Player one",
"subtitles": [
{
"name": "Joe"
}
],
"time": "2016-11-08T16:03:08.957Z",
"products": [
"Xbox"
]
},
{
"header": "Player",
"title": "Player two",
"subtitles": [
{
"name": "Jim"
}
],
"time": "2016-11-08T16:03:08.957Z",
"products": [
"Xbox"
]
},
{
"header": "Player",
"title": "Player three",
"subtitles": [
{
"name": "Chelsea"
}
],
"time": "2016-11-08T16:03:08.957Z",
"products": [
"PC"
]
},
{
"header": "Player",
"title": "Player one",
"subtitles": [
{
"name": "Joe"
}
],
"time": "2016-11-08T16:03:08.957Z",
"products": [
"Xbox"
]
}
]
您可以使用另一个对象为每个唯一的玩家头衔保存一个 属性,然后每次遇到相同的头衔时增加此 属性 的值:
const counter = {};
for(const player in players) {
// checks whether property named as player already exists
if(!counter[player.title]) {
// Create new property, named as player, with 1 as value
counter[player.title] = 1;
} else {
// Increment property value
counter[player.title]++;
}
}
for(const player in counter) {
console.log(`The player ${player} is recorded ${counter[counter]} times`);
}
一种方法是结合使用 .reduce(..)
and .sort(..)
.
这是一个例子:
const players = [
{
"header": "Player",
"title": "Player one",
"subtitles": [
{
"name": "Joe"
}
],
"time": "2016-11-08T16:03:08.957Z",
"products": [
"Xbox"
]
},
{
"header": "Player",
"title": "Player two",
"subtitles": [
{
"name": "Jim"
}
],
"time": "2016-11-08T16:03:08.957Z",
"products": [
"Xbox"
]
},
{
"header": "Player",
"title": "Player one",
"subtitles": [
{
"name": "Joe"
}
],
"time": "2016-11-08T16:03:08.957Z",
"products": [
"Xbox"
]
},
{
"header": "Player",
"title": "Player two",
"subtitles": [
{
"name": "Jim"
}
],
"time": "2016-11-08T16:03:08.957Z",
"products": [
"Xbox"
]
},
{
"header": "Player",
"title": "Player three",
"subtitles": [
{
"name": "Chelsea"
}
],
"time": "2016-11-08T16:03:08.957Z",
"products": [
"PC"
]
},
{
"header": "Player",
"title": "Player one",
"subtitles": [
{
"name": "Joe"
}
],
"time": "2016-11-08T16:03:08.957Z",
"products": [
"Xbox"
]
}
]
const result = players.reduce((a, c) => {
const found = a.find((o) => o.title === c.title);
if (found) {
found.count++;
return a;
}
a.push({ title: c.title, subtitles: c.subtitles, count: 1 });
return a;
}, []).sort((a, b) => a.count - b.count);
console.log(result.map((o, i) => `Player: ${o.subtitles[0].name} is #${i+1}`));
这样做是首先将数组缩减为仅包含唯一条目(原始对象的简化版本)和每个对象出现次数的数组(count
属性).然后,它根据这个数字对数组进行排序。