【问题标题】:iterating array of objects which is present inside array of objects迭代对象数组中存在的对象数组
【发布时间】:2019-03-20 08:47:30
【问题描述】:

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

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

【问题讨论】:

  • 泰米尔语。我已经整理并创建了 sn-p。您能否添加所需的输出格式。
  • 看起来代码有效。您期望不同的输出或只是遇到了一些错误?喜欢来自 redux 的异步数据?
  • @Bibberty 用正确的输出更新了我的问题...感谢您的帮助...您可以发布答案
  • @bird 用正确的输出更新了我的问题...感谢您的帮助

标签: javascript html json reactjs redux


【解决方案1】:

遵循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}`);
    }
  });
});

【讨论】:

    【解决方案2】:

    可能是这样的?这行得通吗?

    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));

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-11
      • 2017-03-29
      • 2019-02-27
      • 2017-07-30
      相关资源
      最近更新 更多