如何通过 javascript 中的 1 个索引将数组 foreach 到数组数据
how to foreach array to array data by 1 index in javascript
嗨,我很困惑 foreach 数组到数组,但是这个 1 索引,我已经在 Javascript 中对 foreach 对象做了很多研究,我尝试了很多方法,但没有任何效果。让我澄清一下 json 我想要实现的目标:
[
{
"name": "Data1",
"color" : "#4473C2",
"data": [
{
"y": 10,
"total":100,
"md": "1",
"name": "Region 1",
"drillup" : 'level2',
"drilldown" : "3",
"next" : "level3"
},{
"y": 20,
"total":100,
"md": "1",
"name": "Region 2",
"drillup" : 'level2',
"drilldown" : "3",
"next" : "level3"
},{
"y": 10,
"total":100,
"md": "1",
"name": "Region 3",
"drillup" : 'level2',
"drilldown" : "3",
"next" : "level3"
},{
"y": 30,
"total":100,
"md": "1",
"name": "Region 4",
"drillup" : 'level2',
"drilldown" : "3",
"next" : "level3"
}
]
}
,{
"name": "Data2",
"color" : "#EB7D30",
"data": [
{
"y": 95,
"total":100,
"md": "1",
"name": "Region 1",
"drillup" : 'level2',
"drilldown" : "3",
"next" : "level3"
},{
"y": 95,
"total":100,
"md": "1",
"name": "Region 2",
"drillup" : 'level2',
"drilldown" : "3",
"next" : "level3"
},{
"y": 95,
"total":100,
"md": "1",
"name": "Region 3",
"drillup" : 'level2',
"drilldown" : "3",
"next" : "level3"
},{
"y": 95,
"total":100,
"md": "1",
"name": "Region 4",
"drillup" : 'level2',
"drilldown" : "3",
"next" : "level3"
}
]
}
]
并且我尝试根据我的一些研究发现来执行 foreach,但结果并不像我想要的或不喜欢我试图实现的。这是我尝试过的脚本:
我的代码中的输出 json =>
[
{
"name": "Data 1",
"color": "#4473C2",
"data": [
{
"y": 23.89,
"total": 124,
"name": "Region 1",
"next": "level_3",
"drilldown": 22
}
]
},
{
"name": "Data 1",
"color": "#EB7D30",
"data": [
{
"y": 18.52,
"total": 40,
"name": "Region 2",
"next": "level_3",
"drilldown": 16
}
]
},
{
"name": "Data 1",
"data": [
{
"y": 12.88,
"total": 51,
"name": "Region 3",
"next": "level_3",
"drilldown": 10
}
]
},
{
"name": "Data 1",
"data":[
{
"y": 7.12,
"total": 28,
"name": "Region 4",
"next": "level_3",
"drilldown": 14
}
]
},
{
"name": "Data 2",
"data": [
{
"y": 94.44,
"total": 17,
"name": "Region 1",
"next": "level_3",
"drilldown": 22
}
]
},
{
"name": "Data 2",
"data": [
{
"y": 100,
"total": 11,
"name": "Region 2",
"next": "level_3",
"drilldown": 16
}
]
},
{
"name": "Data 2",
"data": [
{
"y": 90.91,
"total": 10,
"name": "Region 3",
"next": "level_3",
"drilldown": 10
}
]
},
{
"name": "Data 2",
"data": [
{
"y": 100,
"total": 2,
"name": "Region 4",
"next": "level_3",
"drilldown": 14
},
]
},]
有人可以帮助我获得我想要的数据吗?谢谢
- 首先你必须理解 javascript object 和
json 参考 this
- 并且您可以使用
JSON.parse()
将 json 转换为 javascript 对象
据我了解,您想要的 json 在数据部分有一个对象数组,加上名称和颜色部分是唯一的。
虽然您得到的 json 在数据部分也有一个对象数组,但它在每个数据部分只有 1 个对象加上名称部分不是唯一的。
这是一个可能的解决方案:
dataRegularSecondary.forEach((k,y) => {
let returnDataHasIt = false;
for( let i=0; returnData[i]; i++ )
{
if(returnData[i]['name'] == k.name) // if returnData already has that name
{
// so appending to data only
returnData[i]['data'].push({
y: k.y,
total: k.total_ada,
name: k.name_label,
next: k.next,
drilldown: k.drilldown
});
returnDataHasIt = true;
break;
}
}
if( !returnDataHasIt ) // if returnData did not have that name
{
// adding a new item to returnData
returnData.push({
name: k.name,
color: color_arr[idx++],
data: [{
y: k.y,
total: k.total_ada,
name: k.name_label,
next: k.next,
drilldown: k.drilldown
}]
});
}
});
嗨,我很困惑 foreach 数组到数组,但是这个 1 索引,我已经在 Javascript 中对 foreach 对象做了很多研究,我尝试了很多方法,但没有任何效果。让我澄清一下 json 我想要实现的目标:
[
{
"name": "Data1",
"color" : "#4473C2",
"data": [
{
"y": 10,
"total":100,
"md": "1",
"name": "Region 1",
"drillup" : 'level2',
"drilldown" : "3",
"next" : "level3"
},{
"y": 20,
"total":100,
"md": "1",
"name": "Region 2",
"drillup" : 'level2',
"drilldown" : "3",
"next" : "level3"
},{
"y": 10,
"total":100,
"md": "1",
"name": "Region 3",
"drillup" : 'level2',
"drilldown" : "3",
"next" : "level3"
},{
"y": 30,
"total":100,
"md": "1",
"name": "Region 4",
"drillup" : 'level2',
"drilldown" : "3",
"next" : "level3"
}
]
}
,{
"name": "Data2",
"color" : "#EB7D30",
"data": [
{
"y": 95,
"total":100,
"md": "1",
"name": "Region 1",
"drillup" : 'level2',
"drilldown" : "3",
"next" : "level3"
},{
"y": 95,
"total":100,
"md": "1",
"name": "Region 2",
"drillup" : 'level2',
"drilldown" : "3",
"next" : "level3"
},{
"y": 95,
"total":100,
"md": "1",
"name": "Region 3",
"drillup" : 'level2',
"drilldown" : "3",
"next" : "level3"
},{
"y": 95,
"total":100,
"md": "1",
"name": "Region 4",
"drillup" : 'level2',
"drilldown" : "3",
"next" : "level3"
}
]
}
]
并且我尝试根据我的一些研究发现来执行 foreach,但结果并不像我想要的或不喜欢我试图实现的。这是我尝试过的脚本:
我的代码中的输出 json =>
[
{
"name": "Data 1",
"color": "#4473C2",
"data": [
{
"y": 23.89,
"total": 124,
"name": "Region 1",
"next": "level_3",
"drilldown": 22
}
]
},
{
"name": "Data 1",
"color": "#EB7D30",
"data": [
{
"y": 18.52,
"total": 40,
"name": "Region 2",
"next": "level_3",
"drilldown": 16
}
]
},
{
"name": "Data 1",
"data": [
{
"y": 12.88,
"total": 51,
"name": "Region 3",
"next": "level_3",
"drilldown": 10
}
]
},
{
"name": "Data 1",
"data":[
{
"y": 7.12,
"total": 28,
"name": "Region 4",
"next": "level_3",
"drilldown": 14
}
]
},
{
"name": "Data 2",
"data": [
{
"y": 94.44,
"total": 17,
"name": "Region 1",
"next": "level_3",
"drilldown": 22
}
]
},
{
"name": "Data 2",
"data": [
{
"y": 100,
"total": 11,
"name": "Region 2",
"next": "level_3",
"drilldown": 16
}
]
},
{
"name": "Data 2",
"data": [
{
"y": 90.91,
"total": 10,
"name": "Region 3",
"next": "level_3",
"drilldown": 10
}
]
},
{
"name": "Data 2",
"data": [
{
"y": 100,
"total": 2,
"name": "Region 4",
"next": "level_3",
"drilldown": 14
},
]
},]
有人可以帮助我获得我想要的数据吗?谢谢
- 首先你必须理解 javascript object 和 json 参考 this
- 并且您可以使用
JSON.parse()
将 json 转换为 javascript 对象
据我了解,您想要的 json 在数据部分有一个对象数组,加上名称和颜色部分是唯一的。 虽然您得到的 json 在数据部分也有一个对象数组,但它在每个数据部分只有 1 个对象加上名称部分不是唯一的。
这是一个可能的解决方案:
dataRegularSecondary.forEach((k,y) => {
let returnDataHasIt = false;
for( let i=0; returnData[i]; i++ )
{
if(returnData[i]['name'] == k.name) // if returnData already has that name
{
// so appending to data only
returnData[i]['data'].push({
y: k.y,
total: k.total_ada,
name: k.name_label,
next: k.next,
drilldown: k.drilldown
});
returnDataHasIt = true;
break;
}
}
if( !returnDataHasIt ) // if returnData did not have that name
{
// adding a new item to returnData
returnData.push({
name: k.name,
color: color_arr[idx++],
data: [{
y: k.y,
total: k.total_ada,
name: k.name_label,
next: k.next,
drilldown: k.drilldown
}]
});
}
});