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

标签 javascript html json reactjs redux

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

应打印的每个对象的示例 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);
});

最佳答案

遵循map()filter()的定义

The map() method creates a new array with the results of calling a provided function on every element in the calling array.

The filter() method creates a new array with all elements that pass the test implemented by the provided function.

这两个方法都返回一个新数组,它必须克隆一个新对象,但你不需要它。您所需要的只是 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}`);
    }
  });
});

关于javascript - 迭代对象数组中存在的对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55251618/

相关文章:

javascript - 从 AngularJS 中 $scope 上定义的另一个函数调用 $scope 上定义的函数

javascript - 是否可以使用 websocket 传输数据

javascript - chop 文本以适应 3 行并在 Html 中显示三个点

php - 使用带有两个 </form> 和提交按钮的 html 表单?

c# - 使用多行处理 Json 反序列化

javascript - 如何使用多个变量名称作为对象名称?

javascript - 你能在 YUI 数据表的每个单元格中呈现任意 HTML 吗?

html - 限制选择框列表长度 - 添加滚动条

php - 如何以数据表格式显示 JSON?

java - 将 JSON 数组解析为 Java 对象