简体   繁体   English

如何根据对象的值将对象数组拆分为另一个数组?

[英]How to split an Array of Objects into another Arrays based on their values?

I need to separate the data from an Array depending on the player-id.我需要根据玩家 ID 将数据从数组中分离出来。 The database I've got looks like this:我得到的数据库是这样的:

var database =[
 {
  player: 1,
  level: 1,
  score: 20
 },
 {
  player: 2,
  level: 1,
  score: 25
 },
 {
  player: 3,
  level: 1,
  score: 7
 },
 {
  player: 1,
  level: 2,
  score: 25
 },
 {
  player: 2,
  level: 2,
  score: 7
 }
]

Little by little, new objects containing new players will be added and old objects will disappear, as the database is limited to 40 entries.一点一点地,包含新玩家的新对象将被添加,旧对象将消失,因为数据库限制为 40 个条目。 This means that the database changes dynamically.这意味着数据库是动态变化的。

My goal is to push the objects into new Arrays depending on the player-id.我的目标是根据玩家 ID 将对象推送到新的数组中。 So the result has to look like this:所以结果必须是这样的:

var player1 = [
 {
  player: 1,
  level: 1,
  score: 20
 },
 {
  player: 1,
  level: 2,
  score: 25
 }
]

var player2 = [
 {
  player: 2,
  level: 1,
  score: 25
 },
 {
  player: 2,
  level: 2,
  score: 7
 }
]

var player3 = [
 {
  player: 3,
  level: 1,
  score: 7
 }
]

Although it's possible to create global variables with dynamic names, it's not possible to create non-global variables with dynamic names, and the global environment is already sufficiently crowded that creating a bunch of new globals is not best practice.虽然可以用动态名称创建全局变量,但不能用动态名称创建非全局变量,而且全局环境已经足够拥挤,创建一堆新的全局变量不是最佳实践。

Instead, use a Map :相反,使用Map

const playerArrays = new Map();
for (const entry of database) {
    const key = `player${entry.player}`;
    const array = playerArrays.get(key);
    if (!array ) {
        // Haven't seen this one yet, add a new array
        playerArrays.set(key, [entry]);
    } else {
        // We have the array, add to it
        array.push(entry);
    }
}

Then you get an individual array by using playerArrays.get("player1") and such.然后你通过使用playerArrays.get("player1")等获得一个单独的数组。

Live Example:现场示例:

 var database = [{ player: 1, level: 1, score: 20 }, { player: 2, level: 1, score: 25 }, { player: 3, level: 1, score: 7 }, { player: 1, level: 2, score: 25 }, { player: 2, level: 2, score: 7 } ]; const playerArrays = new Map(); for (const entry of database) { const key = `player${entry.player}`; const array = playerArrays.get(key); if (!array ) { // Haven't seen this one yet, add a new array playerArrays.set(key, [entry]); } else { // We have the array, add to it array.push(entry); } } console.log("player1:", playerArrays.get("player1"));
 .as-console-wrapper { max-height: 100% !important; }

First, you can convert the array to an object to improve the access and then you can assign object properties to variables:首先,您可以将数组转换为对象以改进访问,然后您可以将对象属性分配给变量:

 const database = [ { player: 1, level: 1, score: 20 }, { player: 2, level: 1, score: 25 }, { player: 3, level: 1, score: 7 }, { player: 1, level: 2, score: 25 }, { player: 2, level: 2, score: 7 } ]; const { player1, player2, player3 } = database.reduce((acc, el) => { if (!('player' + el.player in acc)) acc['player' + el.player] = [el]; else acc['player' + el.player].push(el); return acc }, {}); console.log(player1, player2, player3);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM