首页 > 解决方案 > 如何在javascript中通过1个索引将数组foreach数组数据

问题描述

嗨,我对 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
        },
    ]
},]  

有人可以帮助我获得我想要的数据吗?谢谢

标签: javascriptarraysjsonobjectforeach

解决方案


据我了解,您想要的 json 在数据部分中有一组对象,加上名称和颜色部分是唯一的。虽然您得到的 json 在数据部分也有一个对象数组,但每个数据部分只有一个对象,而且名称部分不是唯一的。

这是一个可能的解决方案:

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

});

推荐阅读