简体   繁体   English

遍历嵌套的JSON对象以查找值id

[英]Loop through a nested JSON object to find a value id

    "results": {
    "data": {
        "facets": {
            "60749428": {
                "id": 60749428,
                "name": "KC Content Content Kind"
            },
            "60750276": {
                "id": 60750276,
                "name": "KC Content Product Version"
            },
            "69107204": {
                "id": 69107204,
                "name": "KC Video Audience"
            },
            "69127027": {
                "id": 69127027,
                "name": "KC Content Kind ID"
            }
        }
    }
}

I want to loop through this nested json object by going into the facet object and say if the name attribute is "KC Content Kind ID" then return the id for that corresponding name attribute 我想通过进入facet对象来遍历此嵌套的json对象,并说如果name属性是“ KC Content Kind ID”,则返回该对应name属性的id

So after getting my api call with postman I was trying to get the corresponding id of the "KC Content Kind ID" in my success function this way, but since its not an array I was wondering if each will work in jquery. 因此,在与邮递员进行api调用后,我试图以此方式在成功函数中获取“ KC内容类型ID”的相应ID,但是由于它不是数组,所以我想知道它们是否都可以在jquery中工作。

    //Get Available Kinds
function getAvailableKinds() {
    $.ajax({
        url: csexe + "/api/v2/facets/" +getLocationId(),
        dataType: "json",
        type: "GET",
        beforeSend: function(xhr) {
            xhr.setRequestHeader ("OTCSticket", getAuthToken());
        },
        success: function(response) {
            var obj = response.results.data.facets;
            $.each(obj, function(item, value){
                 if ( value == 'KC Content Kind ID') {
                     var idRequired = obj.id;
                 }
            });
        },
        error: function(jqXHR, textStatus, errorThrown){
            alert("An error occurred... Look at the console");
            $("body").html('<p>status code: '+jqXHR.status+'</p><p>Error Thrown: ' + errorThrown + '</p><p>Response Text:</p><div>'+jqXHR.responseText + '</div>');
        }
    });

Just parse the string over and then do a simple loop. 只需解析字符串,然后执行简单的循环即可。

var jsonObj = (JSON.parse("your json here")).data.facets;

for (i = 0; i<jsonObj.length;i++)
{
    if(jsonObj[i].name == "KC Content Kind ID")
        return jsobObj[i].id;
}

I think the easiest way to achieve this would be by using Object.values function in conjunction with Array.prototype.filter . 我认为最简单的方法是将Object.values函数与Array.prototype.filter结合使用。 You can then take the first item from the array returned by the filter method (since each ID should be unique) and display it's ID. 然后,您可以从filter方法返回的数组中获取第一项(因为每个ID应该是唯一的)并显示其ID。

 const o = { "results": { "data": { "facets": { "60749428": { "id": 60749428, "name": "KC Content Content Kind" }, "60750276": { "id": 60750276, "name": "KC Content Product Version" }, "69107204": { "id": 69107204, "name": "KC Video Audience" }, "69127027": { "id": 69127027, "name": "KC Content Kind ID"}}}}}; const [a] = Object.values(o.results.data.facets).filter(f => f.name == "KC Content Kind ID"); console.log(a.id); 

you can use Object.keys and find 您可以使用Object.keysfind

 const obj = {"results": {"data": {"facets": {"60749428": {"id": 60749428,"name": "KC Content Content Kind"},"60750276": {"id": 60750276,"name": "KC Content Product Version"},"69107204": {"id": 69107204,"name": "KC Video Audience"},"69127027": {"id": 69127027,"name": "KC Content Kind ID"}}}}}; const facets = obj.results.data.facets; const result = Object.keys(facets).find(v => facets[v].name === 'KC Content Kind ID'); //your object keys are equal to id, you can just return key console.log(result); // if your object keys can be different from id you can do this console.log(facets[result].id); 

 var obj = { "results": { "data": { "facets": { "60749428": { "id": 60749428, "name": "KC Content Content Kind" }, "60750276": { "id": 60750276, "name": "KC Content Product Version" }, "69107204": { "id": 69107204, "name": "KC Video Audience" }, "69127027": { "id": 69127027, "name": "KC Content Kind ID" } } } } }; let facets = obj.results.data.facets; let id; for(let key in facets){ if(facets[key].name == 'KC Content Kind ID'){ id = facets[key].id; break; } } console.log(id); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM