【问题标题】:how to foreach array to array data by 1 index in javascript如何在javascript中通过1个索引将数组foreach数组数据
【发布时间】:2021-11-10 05:10:20
【问题描述】:

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

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

【问题讨论】:

  • 你好它的返回变量returnData
  • 请告诉我们函数informationModel.getDataREgularShelfSecondaryDisplay(data)返回的内容

标签: javascript arrays json object foreach


【解决方案1】:
  • 首先你必须了解javascript对象和 json 参考this
  • 您可以使用JSON.parse()将json转换为javascript对象

【讨论】:

    【解决方案2】:

    据我了解,您想要的 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
                }]
            });
        }
    
    });
    

    【讨论】:

    • 感谢我 gbu 兄弟的回答
    猜你喜欢
    • 2021-10-13
    • 2020-03-29
    • 1970-01-01
    • 1970-01-01
    • 2021-11-09
    • 2013-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多