gpt4 book ai didi

javascript - 在数组中迭代数组并显示 playerId、playerName 和 playerCategory

转载 作者:太空宇宙 更新时间:2023-11-04 15:45:54 25 4
gpt4 key购买 nike

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

更新2:

  • 现在我的数据被移动到 targetData 数组中。所以我的 json 结构改变了。
  • 我使用了相同的代码并进行了迭代,但仍然没有得到值。
  • 你能告诉我如何获取 playerName 和 playerCategory
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"
}

更新1:

  • 假设如果我们在一个数组中有更多的数组,我们是否可以在不给出索引的情况下进行迭代。
  • 你能告诉我吗,因为它可以帮助我处理复杂的数据
{
"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。
  • 当我做第一张 map 时,我得到的值是
    console.log('bunessEntityProvidersValues 值 ---->', bunessEntityProvidersValues);
  • 但是当我在一个数组中做 next map 时,我变得不确定。console.log('combinedEntityProvidersValues 值 ---->', 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);

最佳答案

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

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

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

关于javascript - 在数组中迭代数组并显示 playerId、playerName 和 playerCategory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55836939/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com