简体   繁体   English

递归 function 循环通过 object 和嵌套 object 与 ZDE9B9ED78D7E2E1DCEEFFEE780E2F9

[英]recursive function to loop through object and nested object with javascript

to update my question I would like to be able to access each value on objects and nested objects but II don't know how to do this.为了更新我的问题,我希望能够访问对象和嵌套对象的每个值,但我不知道该怎么做。 I tried first with a foreach loop and a for loop but I didn't know how to get "comment" for example我首先尝试了一个 foreach 循环和一个 for 循环,但我不知道如何获得“评论”,例如

data.forEach((element) => {
    const name = element.restaurantName;
    const address = element.address;
    for(let i = 0; i < data.length; i++) {
        const comment = element.ratings[i].comment;
    }
    init_resto(name, address, comment);
});

var list_group = document.getElementById('list-group'); 

function init_resto(name, address, comment) {
    var liElt = document.createElement('li');
    liElt.innerHTML = 
    "<li class=\"list-group-item\">" + name + "</li>" +  
    "<li class=\"list-group-item\">" + address + "</li>" +
    "<li class=\"list-group-item\">" + comment + "</li>";  
    list_group.appendChild(liElt);
}

My expected output:我预期的 output:

"branco"
"39 Rue des Petites Écuries, 75010 Paris"
"Un excellent restaurant, j'y reviendrai ! Par contre il vaut mieux aimer la viande."
Tout simplement mon restaurant préféré !

"Babalou"
"address":"4 Rue Lamarck, 75018 Paris"
"Une minuscule pizzeria délicieuse cachéejuste à côté du Sacré choeur !"
...

Here is my datas:这是我的数据:

[
    {
       "restaurantName":"Bronco",
       "address":"39 Rue des Petites Écuries, 75010 Paris",
       "lat":48.8737815,
       "long":2.3501649,
       "ratings":[
          {
             "stars":4,
             "comment":"Un excellent restaurant, j'y reviendrai !Par contre il vaut mieux aimer la viande."
          },
          {
             "stars":5,
             "comment":"Tout simplement mon restaurant préféré !"
          }
       ]
    },
    {
       "restaurantName":"Babalou",
       "address":"4 Rue Lamarck, 75018 Paris",
       "lat":48.8865035,
       "long":2.3442197,
       "ratings":[
          {
             "stars":5,
             "comment":"Une minuscule pizzeria délicieuse cachéejuste à côté du Sacré choeur !"
          },
          {
             "stars":3,
             "comment":"J'ai trouvé ça correct, sans plus"
          }
       ]
    }
 ]

then I tried with a recursive function which can be a better way but I don't know how to get all values without overriding these values:然后我尝试使用递归 function 这可能是一种更好的方法,但我不知道如何在不覆盖这些值的情况下获取所有值:

function recurse(obj) {
    let value= "";
    for (let property in obj) { 
        value = obj[property];

        if(typeof obj[property] == "object" && obj[property] !== null) { 
            value = recurse(obj[property]);  
        }

    }
    return value;
}

Comments have explained why your code is returning only a single value.评论解释了为什么您的代码只返回一个值。

It's pretty hard to tell for sure what you're looking to do.很难确定您要做什么。 If what you want is just to collect the leaf values of your object tree, then this simple recursion should do it:如果您想要的只是收集 object 树的叶值,那么这个简单的递归应该可以做到:

 const getValues = (obj) => Object (obj) === obj? Object.values (obj).flatMap (getValues): obj const input = [{restaurantName: "Bronco", address: "39 Rue des Petites Écuries, 75010 Paris", lat: 48.8737815, long: 2.3501649, ratings: [{stars: 4, comment: "Un excellent restaurant, j'y reviendrai.Par contre il vaut mieux aimer la viande,"}: {stars, 5: comment, "Tout simplement mon restaurant préféré:"}]}, {restaurantName: "Babalou", address, "4 Rue Lamarck: 75018 Paris". lat, 48:8865035. long, 2:3442197: ratings, [{stars: 5, comment: "Une minuscule pizzeria délicieuse cachéejuste à côté du Sacré choeur,"}: {stars, 3. comment. "J'ai trouvé ça correct, sans plus"}]}] console .log (input .map (getValues))
 .as-console-wrapper {max-height: 100%;important: top: 0}

(I find Object (obj) === obj a cleaner typecheck than doing typeof .) (我发现Object (obj) === obj比做typeof更干净。)

But if that is not your expected output, please edit the question to include the expected output.但如果这不是您预期的 output,请编辑问题以包含预期的 output。

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

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