对象数组中存在的迭代对象数组

时间:2019-03-19 23:51:41

标签: javascript html json reactjs redux

children.BallAdequacy是一个对象数组。playerRank内部是一个对象数组。 从每个playerRank数组中,我需要在控制台中分别显示Ball和playerHeight值。 我使用了地图和过滤器方法,但仍然无法为每个对象打印出球和playerHeight。  你能告诉我如何解决它。 在下面提供我的代码段和数据

应该为每个对象打印的示例 222 ---> HEIGHT,ww22w22 ---> HEIGHT等

let children = {
  BallAdequacy: [{
      "flight": "dd",

      "specialty": "ff",

      "playerRank": [{
          "Ball": "222",
          "playerHeight": "HEIGHT"
        },
        {
          "Ball": "ddeeeew",
          "playerHeight": "NON-HEIGHT"
        },
      ],
      "hospitalPrivilege": []
    },
    {
      "flight": "kkk",
      "specialty": "ff",

      "playerRank": [{
          "Ball": "kfkf",
          "playerHeight": "HEIGHT"
        },
        {
          "Ball": "All",
          "playerHeight": "NON-HEIGHT"
        }
      ],
      "hospitalPrivilege": []
    }
  ]
};


children.BallAdequacy.map(status => {
  console.log("status.playerRank--->", status.playerRank);
  status.playerRank.filter(game => {
    //console.log("game.playerHeight--->", game.playerHeight);
    if (game.playerHeight === 'HEIGHT') {
      console.log("after if --->", game);
      console.log("after if --->", game.Ball);

    }
    // (game.playerHeight === 'HEIGHT')
    //console.log("outsidei f--->", game);
  });
  console.log("after filter status.playerRank--->", status.playerRank);
  //BallList = getBalls(status.playerRank);
});

2 个答案:

答案 0 :(得分:2)

遵循map()filter()的定义

  

map()方法创建一个新数组,其结果是在调用数组中的每个元素上调用提供的函数。

     

filter()方法将创建一个新数组,其中包含所有通过提供的功能实现的测试的元素。

两个方法都返回一个新数组,它必须克隆一个新对象,但是您不需要这样做。您所需要做的只是console.log,并且没有更改数据。您应该改用forEach。并删除一些不必要的控制台。

let children = {
  BallAdequacy: [{
      "flight": "dd",

      "specialty": "ff",

      "playerRank": [{
          "Ball": "222",
          "playerHeight": "HEIGHT"
        },
        {
          "Ball": "ww22w22",
          "playerHeight": "HEIGHT"
        },

        {
          "Ball": "wwwww",
          "playerHeight": "NON-HEIGHT"
        },
        {
          "Ball": "ddeeeew",
          "playerHeight": "NON-HEIGHT"
        },

        ,
        {
          "Ball": "All",
          "playerHeight": "NON-HEIGHT"
        }
      ],
      "hospitalPrivilege": []
    },
    {
      "flight": "kkk",
      "specialty": "ff",

      "playerRank": [{
          "Ball": "kfkf",
          "playerHeight": "HEIGHT"
        },

        {
          "Ball": "iioioo",
          "playerHeight": "HEIGHT"
        },
        {
          "Ball": "24jk",
          "playerHeight": "NON-HEIGHT"
        },


        {
          "Ball": "All",
          "playerHeight": "NON-HEIGHT"
        }
      ],
      "hospitalPrivilege": []
    }
  ]
};





children.BallAdequacy.forEach(status => {
  status.playerRank.forEach(game => {
    if (game.playerHeight === 'HEIGHT') {
      console.log(`${game.Ball} ---> ${game.playerHeight}`);
    }
  });
});

答案 1 :(得分:0)

也许是这样?这行得通吗?

let children = {
  BallAdequacy: [{
      "flight": "dd",

      "specialty": "ff",

      "playerRank": [{
          "Ball": "222",
          "playerHeight": "HEIGHT"
        },
        {
          "Ball": "ww22w22",
          "playerHeight": "HEIGHT"
        },

        {
          "Ball": "wwwww",
          "playerHeight": "NON-HEIGHT"
        },
        {
          "Ball": "ddeeeew",
          "playerHeight": "NON-HEIGHT"
        },
        {
          "Ball": "All",
          "playerHeight": "NON-HEIGHT"
        }
      ],
      "hospitalPrivilege": []
    },
    {
      "flight": "kkk",
      "specialty": "ff",

      "playerRank": [{
          "Ball": "kfkf",
          "playerHeight": "HEIGHT"
        },

        {
          "Ball": "iioioo",
          "playerHeight": "HEIGHT"
        },
        {
          "Ball": "24jk",
          "playerHeight": "NON-HEIGHT"
        },


        {
          "Ball": "All",
          "playerHeight": "NON-HEIGHT"
        }
      ],
      "hospitalPrivilege": []
    }
  ]
};

let output = children.BallAdequacy.map(s => s.playerRank.map((a) => `${a.Ball}-->${a.playerHeight}`).join());

output.forEach(c => console.log(c));