我想知道是否可以遍历包含数组的对象数组来查找数组

时间:2018-11-29 19:54:13

标签: javascript arrays postman

是否可以遍历包含数组的对象数组来查找特定数组。因此,换句话说,我已经有一个变量a,其值是“ 60749428”,因此我想说的是在“可用”数组中找到一个“ 60749428”。这是下面的代码示例:

    "values": {
            "available": [
                {
                    "60750276": [
                        {
                            "count": 11,
                            "name": "16",
                            "percentage": 84.6153846153846,
                            "value": "16"
                        },
                        {
                            "count": 11,
                            "name": "16.0.1",
                            "percentage": 84.6153846153846,
                            "value": "16.0.1"
                        },
                        {
                            "count": 12,
                            "name": "16.2",
                            "percentage": 92.3076923076923,
                            "value": "16.2"
                        },
                        {
                            "count": 7,
                            "name": "16.2.4",
                            "percentage": 53.8461538461538,
                            "value": "16.2.4"
                        }
                    ]
                },
                {
                    "69127027": [
                        {
                            "count": 8,
                            "name": "65",
                            "percentage": null,
                            "value": "65"
                        },
                        {
                            "count": 4,
                            "name": "69",
                            "percentage": null,
                            "value": "69"
                        }
                    ]
                },
                {
                    "60749428": [
                        {
                            "count": 8,
                            "name": "How To",
                            "percentage": 61.5384615384615,
                            "value": "How To"
                        },
                        {
                            "count": 4,
                            "name": "Training",
                            "percentage": 30.7692307692308,
                            "value": "Training"
                        }
                    ]
                }
            ]
        }

1 个答案:

答案 0 :(得分:1)

看一下代码示例,看来您可以使用Array.find来实现这一目标

let values = {            "available": [                {                    "60750276": [                        {                            "count": 11,                            "name": "16",                            "percentage": 84.6153846153846,                            "value": "16"                        },                        {                            "count": 11,                            "name": "16.0.1",                            "percentage": 84.6153846153846,                            "value": "16.0.1"                        },                        {                            "count": 12,                            "name": "16.2",                            "percentage": 92.3076923076923,                            "value": "16.2"                        },                        {                            "count": 7,                            "name": "16.2.4",                            "percentage": 53.8461538461538,                            "value": "16.2.4"                        }                    ]                },                {                    "69127027": [                        {                            "count": 8,                            "name": "65",                            "percentage": null,                            "value": "65"                        },                        {                            "count": 4,                            "name": "69",                            "percentage": null,                            "value": "69"                        }                    ]                },                {                    "60749428": [                        {                            "count": 8,                            "name": "How To",                            "percentage": 61.5384615384615,                            "value": "How To"                        },                        {                            "count": 4,                            "name": "Training",                            "percentage": 30.7692307692308,                            "value": "Training"                        }                    ]                }            ]        }
        
let res = values.available.find(d => d[60749428])

console.log(res)