简体   繁体   English

使用Javascript提取JSON对象值/键而无需使用硬编码键

[英]Extract JSON object values/keys without using hardcoded keys using Javascript

 cart = {
 "Items": 3,
 "Item": {
      "Apple iPhone 5S": {
              "productId": 688,
              "url": "http://website.com/phone_iphone5s.html",
              "price": 299.99
        },
      "Solio Mono Solar Charger": {
              "productId": 655,
              "url": "http://website.com/solio_charger.html",
              "price": 29.95
        },
       "24 Month Warranty Package": {
              "productId": 681,
              "url": "http://website.com/24_month_warranty.html",
              "price": 129.95
        }
  },
"Total": 459.89
 }

I want to write a JavaScript function that outputs the total number of items, each item with price, and the total value of the shopping cart in the browser dev tools console. 我想编写一个JavaScript函数,该函数输出项目总数,带有价格的每个项目以及浏览器开发工具控制台中购物车的总价值。 Eg : Need to select Apple Iphone 5S and its price, Solio Mono Solar Charger and its price etc . 例如: 需要选择Apple Iphone 5S及其价格,Solio Mono太阳能充电器及其价格等 Without using hardcoded keys (eg “Apple iPhone 5S“, “Solio Mono Solar Charger“). 无需使用硬编码键 (例如“ Apple iPhone 5S”,“ Solio Mono Solar Charger”)。 Is there any way ?? 有什么办法吗??

Is it possible to get in this format ?? 是否有可能采用这种格式?

   Items: 3 
    - Apple iPhone 5S ($299.99) 
    - Solio Mono Solar Charger ($29.95) 
    - 24 Month Warranty Package ($129.95)
   Total: 459.89 

are you looking for something like this 您在寻找这样的东西吗

var str = `Items: ${cart.Items} `;
Object.keys(cart.Item).forEach((key) => {
  str += `- ${key} (${cart.Item[key].price}) `
})

str +=  `Total: ${cart.Total}`
console.log(str)

 var cart = { "Items": 3, "Item": { "Apple iPhone 5S": { "productId": 688, "url": "http://website.com/phone_iphone5s.html", "price": 299.99 }, "Solio Mono Solar Charger": { "productId": 655, "url": "http://website.com/solio_charger.html", "price": 29.95 }, "24 Month Warranty Package": { "productId": 681, "url": "http://website.com/24_month_warranty.html", "price": 129.95 } }, "Total": 459.89 } var str = `Items: ${cart.Items} `; Object.keys(cart.Item).forEach((key) => { str += `- ${key} (${cart.Item[key].price}) ` }) str += `Total: ${cart.Total}` console.log(str) 

You can loop through them and grab the data wanted like this. 您可以遍历它们并像这样获取所需的数据。

You can edit it to fit whatever you need. 您可以对其进行编辑以适合您的任何需求。

cart = {
 "Items": 3,
 "Item": {
      "Apple iPhone 5S": {
              "productId": 688,
              "url": "http://website.com/phone_iphone5s.html",
              "price": 299.99
        },
      "Solio Mono Solar Charger": {
              "productId": 655,
              "url": "http://website.com/solio_charger.html",
              "price": 29.95
        },
       "24 Month Warranty Package": {
              "productId": 681,
              "url": "http://website.com/24_month_warranty.html",
              "price": 129.95
        }
  },
"Total": 459.89
 }

for (var key in cart.Item) {
    if (cart.Item.hasOwnProperty(key)) {
        console.log(key + " " + cart.Item[key]);
    }
}

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

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