【问题标题】:JavaScript get value from nested object [duplicate]JavaScript从嵌套对象中获取值[重复]
【发布时间】:2018-05-10 05:48:45
【问题描述】:

如果这是我的对象:

var obj = {
bakery1: {
    small: {
        name: "Small cookie",
        price: 0.75;
    }
    large: {
        name: "Large cookie",
        price: 3.00;
    }
}
bakery2: {
    small: {
        name: "Small cookie",
        price: 1.00;
    }
    large: {
        name: "Large cookie",
        price: 4.00;
    }
}
};

我将如何制作一个将每个价格打印到控制台的循环?

而当这个值被打印出来的时候,有没有办法追踪名字呢?

例如,如果输出是 3.00,我将如何创建一个函数来给我与 3.00 价格对应的名称?

提前致谢!

【问题讨论】:

    标签: javascript object for-loop nested nested-loops


    【解决方案1】:

    递归函数。

    function objSearch(serObj){
        // For loop through the object.
        for( prop in serObj ){
            // If the property is price get the price.
            if(prop === 'price'){
                // Got the price property now return the value of price.
                console.log('I have price...', prop, '=', serObj[prop]);
                // If the property is an object send it through the function again.
            } else if(typeof serObj[prop] === 'object'){  
                // Pass the nested object.
                objSearch(serObj[prop]);
            }
        }
    }
    
    objSearch(obj);
    

    【讨论】:

      【解决方案2】:

      回答你的问题:

      function printPrice(obj) {
        if (obj.price)
          console.log(obj.name, obj.price)
      
        else for (key in obj)
          printPrice(obj[key])
      }
      
      var obj = {
        "bakery1": {
          "small": {
            "name": "Small cookie",
            "price": 0.75
          },
          "large": {
            "name": "Large cookie",
            "price": 3
          }
        },
        "bakery2": {
          "small": {
            "name": "Small cookie",
            "price": 1
          },
          "large": {
            "name": "Large cookie",
            "price": 4
          }
        }
      };
      
      printPrice(obj)

      但是,对于这些类型的集合,使用数组可能会更好。例如,我们可以这样做:

      var bakeries = [
        {
          name: 'Bakery 1',
          menu: [
            {name: 'small cookie', price: 0.75},
            {name: 'large cookie', price: 3.00}
          ]
        },
      
        {
          name: 'Bakery 2',
          menu: [
            {name: 'small cookie', price: 1.00},
            {name: 'large cookie', price: 4.00}
          ]
        }
      ]
      

      这使我们能够更好地组织每个项目的特定属性。在这种情况下,打印价格的代码变得更加简单:

      bakeries
        .map(bakery => bakery.menu)
        .forEach(menu => menu.map(
          item => console.log(item.name, item.price)))
      

      【讨论】:

        【解决方案3】:

        运行它,看看它是如何工作的

        var obj = {
          bakery1: {
            small: {
              name: "Small cookie",
              price: 0.75
            },
            large: {
              name: "Large cookie",
              price: 3.00
            }
          },
          bakery2: {
            small: {
              name: "Small cookie",
              price: 1.00
            },
            large: {
              name: "Large cookie",
              price: 4.00
            }
          }
        };
        
        Object.keys(obj).map((name) => {
          Object.keys(obj[name]).map((type) => document.write(`${name}-${type}-${obj[name][type].name}-${obj[name][type].price}<br/>`))
        })

        【讨论】:

        • 你能用你的代码解释一下吗?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-18
        • 2018-10-14
        • 2023-03-12
        相关资源
        最近更新 更多