Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何迭代对象以获得不同的结果?_Javascript_Arrays_Object - Fatal编程技术网

Javascript 如何迭代对象以获得不同的结果?

Javascript 如何迭代对象以获得不同的结果?,javascript,arrays,object,Javascript,Arrays,Object,下面是对象数组,我得到了一个新的数组,它由不同回合的队长名字和分数组成,但在所有的数组中都是相同的队长,而我想要的是有队长名字和分数的新数组,但我的代码只给出了一个队长的相同结果,我不知道为什么会发生这样的事情,当然有我有点做错了,但我不明白我想要一个新的阵型,以队长的名字和他们的得分为目标。没有相同的船长一次又一次有相同的记录 let data = [{ game_name: "test101" players_info: {captain: "john"

下面是对象数组,我得到了一个新的数组,它由不同回合的队长名字和分数组成,但在所有的数组中都是相同的队长,而我想要的是有队长名字和分数的新数组,但我的代码只给出了一个队长的相同结果,我不知道为什么会发生这样的事情,当然有我有点做错了,但我不明白我想要一个新的阵型,以队长的名字和他们的得分为目标。没有相同的船长一次又一次有相同的记录

let data = [{
game_name: "test101"
players_info: {captain: "john", stage_name: "fun fin fo", team_members: "John,Jane,jaden"}
response: "this is one response"
round_num: "1"
score: "1"}
,{game_name: "test101"
players_info: {captain: "B", stage_name: "dssd", team_members: "01,02,03,05"}
response: "another test response....!!!"
round_num: "3"
score: "0"},
{game_name: "test101"
players_info: {captain: "B", stage_name: "baj", team_members: "S"}
response: "He is just gorgeous "
round_num: "1"
score: "6"}
,
{game_name: "test101"
players_info: {captain: "B", stage_name: "fun fin fo", team_members: "Its Just Me and myself :)"}
response: "Thats the same joke we had last week"
round_num: "2"
score: "4"}]
let scores = {};
   let new_arr = [];
   var counter = 0;
   for (let i = 0; i < current_data.length; i++) {
     for (let x = current_data.length - 1; x >= 0; x--) {
       if (
         current_data[x].players_info.captain ===
         current_data[i].players_info.captain
       ) {
         scores['captain'] = current_data[x].players_info.captain;
         scores[current_data[x].round_num] = current_data[x].score;
    
       }   
     }
     new_arr.push({ score: scores });
   }
   console.log(new_arr);


(4) [{…}, {…}, {…}, {…}]
0:
score: {1: "6", 2: "4", 3: "0", captain: "B"}
__proto__: Object
1:
score: {1: "6", 2: "4", 3: "0", captain: "B"}
__proto__: Object
2:
score: {1: "6", 2: "4", 3: "0", captain: "B"}
__proto__: Object
3:
score: {1: "6", 2: "4", 3: "0", captain: "B"}
__proto__: Object
length: 4
__proto__: Array(0)
let数据=[{
游戏名称:“test101”
球员信息:{队长:“约翰”,舞台名称:“芬菲”,队员:“约翰,简,杰登”}
回应:“这是一个回应”
轮数:“1”
分数:“1”}
{游戏名称:“test101”
球员信息:{队长:“B”,舞台名称:“dssd”,队员:“01,02,03,05”}
回答:“另一个测试回答….!!!”
轮数:“3”
分数:“0”},
{游戏名称:“test101”
球员信息:{队长:“B”,舞台名称:“baj”,队员:“S”}
回答:“他真漂亮”
轮数:“1”
分数:“6”}
,
{游戏名称:“test101”
球员信息:{队长:“B”,舞台名称:“fun fin fo”,队员:“只有我和我自己:)”}
回答:“这和我们上周开的玩笑一样”
轮数:“2”
分数:“4”}]
让分数={};
让new_arr=[];
var计数器=0;
for(设i=0;i=0;x--){
如果(
当前数据[x]。玩家信息。队长===
当前数据[i]。球员信息。队长
) {
得分['captain']=当前数据[x]。球员信息。队长;
分数[当前数据[x]。舍入数]=当前数据[x]。分数;
}   
}
新的arr.push({score:scores});
}
控制台日志(新建);
(4) [{…}, {…}, {…}, {…}]
0:
得分:{1:6,2:4,3:0,队长:“B”}
__原型:对象
1:
得分:{1:6,2:4,3:0,队长:“B”}
__原型:对象
2:
得分:{1:6,2:4,3:0,队长:“B”}
__原型:对象
三:
得分:{1:6,2:4,3:0,队长:“B”}
__原型:对象
长度:4
__原型:数组(0)

它添加的是同一个队长,因为您只设置了一次分数值,而从不重置它

代码现在将每个队长的分数添加到数组中

let data = [
    {
        game_name: "test101",
        players_info: {
            captain: "john",
            stage_name: "fun fin fo",
            team_members: "John,Jane,jaden"
        },
        response: "this is one response",
        round_num: "1",
        score: "1"
    },
    {
        game_name: "test101",
        players_info: {
            captain: "A",
            stage_name: "dssd",
            team_members: "01,02,03,05"
        },
        response: "another test response....!!!",
        round_num: "3",
        score: "0",
    },
    {
        game_name: "test101",
        players_info: {
            captain: "B",
            stage_name: "baj",
            team_members: "S"
        },
        response: "He is just gorgeous ",
        round_num: "1",
        score: "6",
    },
    {
        game_name: "test101",
        players_info: {
            captain: "C",
            stage_name: "fun fin fo",
            team_members: "Its Just Me and myself :)"
        },
        response: "Thats the same joke we had last week",
        round_num: "2",
        score: "4",
    },
    {
        game_name: "new game",
        players_info: {
            captain: "john",
            stage_name: "fun fin fo",
            team_members: "Its Just Me and myself :)"
        },
        response: "Thats the same joke we had last week",
        round_num: "2",
        score: "4",
    }
];

let scores = {};
let new_arr = [];

for ( i=0; i < data.length; i++ ) {
    
    //Check to see if captain already exists to not make a duplicate entry for the same captain.
    let found = false;
    for( x=0; x < new_arr.length; x++ ) {
        if (new_arr[x].score.captain == data[i].players_info.captain) {
            found = true;
            let temp = new_arr[x];
            temp.score[`Game#${data[i].round_num}`] = data[i].score; //Adding another game entry to the captain.
            new_arr[x]=temp;
        }
    }

    //If the captain doesnt already exist in the new_arr, create a new entry for them.
    if ( found == false ) {
        scores = {}; //Here the captains object is reset.
        scores['captain'] = data[i].players_info.captain;
        scores[`Game#${data[i].round_num}`] = data[i].score;
        new_arr.push({ score: scores });
    }
}

console.log(new_arr);

非常感谢你。我现在知道了,我忘了重置对象,但它仍然将值保存在其中。
[
  { score: { captain: 'john', 'Game#1': '1', 'Game#2': '4' } },
  { score: { captain: 'A', 'Game#3': '0' } },
  { score: { captain: 'B', 'Game#1': '6' } },
  { score: { captain: 'C', 'Game#2': '4' } }
]