迭代数组内的数组并显示playerId,playerName和playerCategory

问题描述 投票:2回答:1

更新3:

调试后找到了答案。提供以下解决方案

let values = {
  "sportsEntitties": [{
      "sportsEntityId": 30085585,
      "sportsEntityName": "490349903434903490",
      "sportsEntityStartDate": "7878787878",
      "sportsEntityEndDate": "erierioerioerioioe",
      "targetData": [{
          "playerName": "490349903434903490",
          "playerCategory": "hjuwerwewkwjke",
          "playerTaxId": "789347893489348934893489823",
          "relationshipStartDate": "2010-07-01"
        },
        {
          "playerName": "490349903434903490",
          "playerCategory": "hjuwerwewkwjke",
          "playerTaxId": "789347893489348934893489823",
          "relationshipStartDate": "2010-07-01"
        },
        {
          "playerName": "490349903434903490",
          "playerCategory": "hjuwerwewkwjke",
          "playerTaxId": "789347893489348934893489823",
          "relationshipStartDate": "2010-07-01"
        }
      ]
    },
    {
      "sportsEntityId": 30077252,
      "sportsEntityName": "uieruieuieruiuier",
      "sportsEntityStartDate": "7878787878",
      "sportsEntityEndDate": "erierioerioerioioe",
      "targetData": [{
          "playerId": 3478347834783478347878347834783478783484818,
          "playerName": "eruieruiiererui",
          "playerCategory": "uuiweruiwerweruiwfuiw",
          "playerTaxId": "789347893489348934893489760",
          "relationshipStartDate": "347834783478347834787834783478347878347-10-30"
        },
        {
          "playerId": 3478347834783478347878347834783478783493294,
          "playerName": "990we9090we90we90",
          "playerCategory": "uuiweruiwerweruiwfuiw",
          "playerTaxId": "789347893489348934893489760",
          "relationshipStartDate": "2010-06-30"
        },
        {
          "playerId": 3478347834783478347878347834783478783493779,
          "playerName": "uieruieuieruiuier",
          "playerCategory": "hjuwerwewkwjke",
          "playerTaxId": "789347893489348934893489760",
          "relationshipStartDate": "2010-07-01"
        }
      ]
    },
    {
      "sportsEntityId": 30085115,
      "sportsEntityName": "cvccvcvcvcvcv",
      "sportsEntityStartDate": "2010-06-30",
      "sportsEntityEndDate": "9999-12-31",
      "targetData": [{
        "playerId": 3478347834783478347878347834783478783493279,
        "playerName": "jkcvjkcdjkcvj",
        "playerCategory": "uuiweruiwerweruiwfuiw",
        "playerTaxId": "27-2695112",
        "relationshipStartDate": "2010-06-30"
      }]
    }

  ],
  "corporateEntityId": "wewewe",
  "corporateEntityName": "wewewewe"
}

let sportsExpnasionHeading = [];


sportsExpnasionHeading = values.sportsEntitties.map(obj => {
  return {
    sportsEntityName: obj.sportsEntityName,
    targetData: obj.targetData.map(obj =>
      ({
        label: `${obj.playerName} | ${obj.playerCategory}`,

        value: obj.playerTaxId,
        category: obj.playerCategory,
        checked: true
      })
    )
  }
});

console.log("sportsExpnasionHeading--->", sportsExpnasionHeading);

UPDATE2:

  • 现在我的数据被移动到targetData数组中。所以我的json结构改变了。
  • 我使用相同的代码并迭代但仍未获得值。
  • 你能告诉我如何获取玩家姓名和玩家类别
const results = Object.values(others).map(obj => {
            return obj[0].playerName + '|' + obj[0].playerCategory
        });

{
  "sportsEntitties": [
    {
      "sportsEntityId": 30085585,
      "sportsEntityName": "490349903434903490",
      "sportsEntityStartDate": "7878787878",
      "sportsEntityEndDate": "erierioerioerioioe",
      "targetData": [
        {
          "playerName": "490349903434903490",
          "playerCategory": "Group",
          "playerTaxId": "789347893489348934893489823",
          "relationshipStartDate": "2010-07-01"
        }
      ]
    },
    {
      "sportsEntityId": 30077252,
      "sportsEntityName": "uieruieuieruiuier",
      "sportsEntityStartDate": "7878787878",
      "sportsEntityEndDate": "erierioerioerioioe",
      "targetData": [
        {
          "playerId": 3478347834783478347878347834783478783484818,
          "playerName": "eruieruiiererui",
          "playerCategory": "Facility",
          "playerTaxId": "789347893489348934893489760",
          "relationshipStartDate": "347834783478347834787834783478347878347-10-30"
        },
        {
          "playerId": 3478347834783478347878347834783478783493294,
          "playerName": "990we9090we90we90",
          "playerCategory": "Facility",
          "playerTaxId": "789347893489348934893489760",
          "relationshipStartDate": "2010-06-30"
        },
        {
          "playerId": 3478347834783478347878347834783478783493779,
          "playerName": "uieruieuieruiuier",
          "playerCategory": "Group",
          "playerTaxId": "789347893489348934893489760",
          "relationshipStartDate": "2010-07-01"
        }
      ]
    },
    {
      "sportsEntityId": 30085115,
      "sportsEntityName": "cvccvcvcvcvcv",
      "sportsEntityStartDate": "2010-06-30",
      "sportsEntityEndDate": "9999-12-31",
      "targetData": [
        {
          "playerId": 3478347834783478347878347834783478783493279,
          "playerName": "jkcvjkcdjkcvj",
          "playerCategory": "Facility",
          "playerTaxId": "27-2695112",
          "relationshipStartDate": "2010-06-30"
        }
      ]
    }

  ],
  "corporateEntityId": "wewewe",
  "corporateEntityName": "wewewewe"
}

UPDATE1:

  • 假设我们在数组中有更多数组,我们可以迭代而不给索引。
  • 能不能让我知道,因为在复杂的数据中它会有所帮助
{
            "sportsEntitties": [
                {
                    "sportsEntityId": 23232323,
                    "sportsEntityName": "wewewewewe",
                    "sportsEntityStartDate": "ewewwewewe",
                    "sportsEntityEndDate": "9999-12-31",
                    "bunessEntityProviders": [
                        {
                            "playerId": 23232323232323,
                            "playerName": "sdssdsdsd",
                            "playerCategory": "Facility",
                            "playerTaxId": "3233223",
                            "relationshipStartDate": "sdsdsdsdsd"
                        }
                    ],
                    "fddfjkjkdfjkdf": [
                        {
                            "playerId": 23232323232323,
                            "playerName": "sdssdsdsd",
                            "playerCategory": "Facility",
                            "playerTaxId": "3233223",
                            "relationshipStartDate": "sdsdsdsdsd"
                        }
                    ],
                    "a": [
                        {
                            "playerId": 23232323232323,
                            "playerName": "sdssdsdsd",
                            "playerCategory": "Facility",
                            "playerTaxId": "3233223",
                            "relationshipStartDate": "sdsdsdsdsd"
                        }
                    ],
                    "bc": [
                        {
                            "playerId": 23232323232323,
                            "playerName": "sdssdsdsd",
                            "playerCategory": "Facility",
                            "playerTaxId": "3233223",
                            "relationshipStartDate": "sdsdsdsdsd"
                        }
                    ],
                     "de": [
                        {
                            "playerId": 23232323232323,
                            "playerName": "sdssdsdsd",
                            "playerCategory": "Facility",
                            "playerTaxId": "3233223",
                            "relationshipStartDate": "sdsdsdsdsd"
                        }
                    ],
                     "jkljkllk": [
                        {
                            "playerId": 23232323232323,
                            "playerName": "sdssdsdsd",
                            "playerCategory": "Facility",
                            "playerTaxId": "3233223",
                            "relationshipStartDate": "sdsdsdsdsd"
                        }
                    ],

                }
            ],
                "corporateEntityId": "2323232323",
                    "corporateEntityName": "ssdsdsd"
        }
  • 我试图在数组内迭代一个数组并显示playerId,playerName和playerCategory。
  • 当我第一次映射时,我得到了价值 console.log('bunessEntityProvidersValues values ---->', bunessEntityProvidersValues);
  • 但是当我在数组中进行下一个映射时,我得到了未定义。 console.log('combinedEntityProvidersValues values ---->', combinedEntityProvidersValues);
  • 你能告诉我如何使用map方法修复它吗?
  • 我调试但仍然无法找到。

样本数据:

{
            "sportsEntitties": [
                {
                    "sportsEntityId": 23232323,
                    "sportsEntityName": "wewewewewe",
                    "sportsEntityStartDate": "ewewwewewe",
                    "sportsEntityEndDate": "9999-12-31",
                    "bunessEntityProviders": [
                        {
                            "playerId": 23232323232323,
                            "playerName": "sdssdsdsd",
                            "playerCategory": "Facility",
                            "playerTaxId": "3233223",
                            "relationshipStartDate": "sdsdsdsdsd"
                        }
                    ]
                }
            ],
                "corporateEntityId": "2323232323",
                    "corporateEntityName": "ssdsdsd"
        }

代码段:

let bunessEntityProvidersValues = [];
let combinedEntityProvidersValues; //= [];



bunessEntityProvidersValues = values.data.sportsEntitties.map(obj => {
    return obj.bunessEntityProviders
})
console.log('bunessEntityProvidersValues values ---->', bunessEntityProvidersValues);
combinedEntityProvidersValues = bunessEntityProvidersValues.map(obj => {
    return obj.playerName + '|' + obj.playerCategory
})
console.log('combinedEntityProvidersValues values ---->', combinedEntityProvidersValues);
javascript html arrays json reactjs
1个回答
4
投票

您为代码获取[ 'undefined|undefined' ]的原因是,当您在Array.map()上运行values.data.sportsEntitties时,它实际上会在新数组中返回结果,每个元素都是您传入的回调函数的结果。因此,bunessEntityProvidersValues是实际上是一个嵌套数组,因为你专门返回包含数组的bunessEntityProviders

你只需要选择0的索引bunessEntityProvidersValues,这是一个数组数组。

const combinedEntityProvidersValues = bunessEntityProvidersValues[0].map(obj => {
  return obj.playerName + '|' + obj.playerCategory
});

这是完整的演示。您可以运行它来查看打印结果。

const values = {
  "sportsEntitties": [{
    "sportsEntityId": 23232323,
    "sportsEntityName": "wewewewewe",
    "sportsEntityStartDate": "ewewwewewe",
    "sportsEntityEndDate": "9999-12-31",
    "bunessEntityProviders": [{
      "playerId": 23232323232323,
      "playerName": "sdssdsdsd",
      "playerCategory": "Facility",
      "playerTaxId": "3233223",
      "relationshipStartDate": "sdsdsdsdsd"
    }]
  }],
  "corporateEntityId": "2323232323",
  "corporateEntityName": "ssdsdsd"
}

const bunessEntityProvidersValues = values.sportsEntitties.map(obj => {
  return obj.bunessEntityProviders
});
//console.log(bunessEntityProvidersValues);

const combinedEntityProvidersValues = bunessEntityProvidersValues[0].map(obj => {
  return obj.playerName + '|' + obj.playerCategory
});
console.log(combinedEntityProvidersValues);

编辑:这是我们如何从您更新的数据解决问题。基本上,我们利用spread syntax来执行object destructuring。首先,我们删除不需要的属性,如sportsEntityIdsportsEntityNamesportsEntityStartDatesportsEntityEndDate,并将其余属性存储到一个名为others的新对象中。然后,我们遍历它以打印所需的输出。

const values = {
  "sportsEntitties": [{
    "sportsEntityId": 23232323,
    "sportsEntityName": "wewewewewe",
    "sportsEntityStartDate": "ewewwewewe",
    "sportsEntityEndDate": "9999-12-31",
    "bunessEntityProviders": [{
      "playerId": 23232323232323,
      "playerName": "sdssdsdsd",
      "playerCategory": "Facility",
      "playerTaxId": "3233223",
      "relationshipStartDate": "sdsdsdsdsd"
    }],
    "fddfjkjkdfjkdf": [{
      "playerId": 23232323232323,
      "playerName": "sdssdsdsd",
      "playerCategory": "Facility",
      "playerTaxId": "3233223",
      "relationshipStartDate": "sdsdsdsdsd"
    }],
    "a": [{
      "playerId": 23232323232323,
      "playerName": "sdssdsdsd",
      "playerCategory": "Facility",
      "playerTaxId": "3233223",
      "relationshipStartDate": "sdsdsdsdsd"
    }],
    "bc": [{
      "playerId": 23232323232323,
      "playerName": "sdssdsdsd",
      "playerCategory": "Facility",
      "playerTaxId": "3233223",
      "relationshipStartDate": "sdsdsdsdsd"
    }],
    "de": [{
      "playerId": 23232323232323,
      "playerName": "sdssdsdsd",
      "playerCategory": "Facility",
      "playerTaxId": "3233223",
      "relationshipStartDate": "sdsdsdsdsd"
    }],
    "jkljkllk": [{
      "playerId": 23232323232323,
      "playerName": "sdssdsdsd",
      "playerCategory": "Facility",
      "playerTaxId": "3233223",
      "relationshipStartDate": "sdsdsdsdsd"
    }],

  }],
  "corporateEntityId": "2323232323",
  "corporateEntityName": "ssdsdsd"
}

const {
  sportsEntityId,
  sportsEntityName,
  sportsEntityStartDate,
  sportsEntityEndDate,
  ...others
} = values.sportsEntitties[0];

const results = Object.values(others).map(obj => {
  return obj[0].playerName + '|' + obj[0].playerCategory
});
/* 
const results = Object.values(others).map(list => {
  return list.map(obj => {
    return obj.playerName + '|' + obj.playerCategory
   })
}); 
*/

console.log(results);

推荐问答