- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
更新 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:
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"
}
console.log('bunessEntityProvidersValues 值 ---->', bunessEntityProvidersValues);
console.log('combinedEntityProvidersValues 值 ---->', combinedEntityProvidersValues);
示例数据:
{
"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 .首先,我们删除不需要的属性,例如 sportsEntityId
、sportsEntityName
、sportsEntityStartDate
、sportsEntityEndDate
,并将其余的存储到一个名为 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/
更新 3: 调试后找到答案。提供以下解决方案 let values = { "sportsEntitties": [{ "sportsEntityId": 30085585,
我是一名优秀的程序员,十分优秀!