仅打印JSON数组中的True元素

时间:2019-10-22 01:50:16

标签: javascript html json

使用Java脚本,我需要根据JSON数组中属性的值将属性添加到html页面的列表中。 数组的格式如下: '

var yelp= {
"attributes": {
    "Take-out": true,
    "Drive-Thru": false,
    "Good For": {
        "dessert": false,
        "latenight": true,
        "lunch": true,
        "dinner": true,
        "brunch": true,
        "breakfast": true
    },
    "Caters": false,
    "Noise Level": "average",
    "Takes Reservations": true,
    "Delivery": false,
    "Ambience": {
        "romantic": true,
        "intimate": false,
        "classy": false,
        "hipster": true,
        "divey": false,
        "touristy": false,
        "trendy": true,
        "upscale": false,
        "casual": true
    },
    "Parking": {
        "garage": false,
        "street": true,
        "validated": false,
        "lot": true,
        "valet": false
    },
    "Has TV": true,
    "Outdoor Seating": true,
    "Attire": "casual",
    "Alcohol": "none",
    "Waiter Service": true,
    "Accepts Credit Cards": true,
    "Good for Kids": true,
    "Good For Groups": true,
    "Price Range": 2
},
"type": "business"

}

基本上,我想打印所有= true的值, 我尝试使用if语句,例如:  if (yelp.attributes.Take-out == true) output.innerHTML+= "Take-out <br>; 它不起作用

3 个答案:

答案 0 :(得分:0)

注意我假设您实际上是在使用yelp对象数组而不是单个yelp对象。如果是这种情况,请注意,我将yelp对象包装在方括号[]中,使其成为数组。

例如,当您使用带有连字符或空格的json属性名称时,就不能像yelp.attributes.Take-out那样访问它们。您必须使用yelp.attributes['Take-out']这样的括号符号,否则,JavaScript会将其转换为数学形式:yelp.attributes.Take - out

如果不是,则不尝试遍历对象,而直接访问属性。 (请参见第二个示例)

您可以使用Array.filter来查找所需内容:

var yelp = [{
  "attributes": {
    "Take-out": true,
    "Drive-Thru": false,
    "Good For": {
      "dessert": false,
      "latenight": true,
      "lunch": true,
      "dinner": true,
      "brunch": true,
      "breakfast": true
    },
    "Caters": false,
    "Noise Level": "average",
    "Takes Reservations": true,
    "Delivery": false,
    "Ambience": {
      "romantic": true,
      "intimate": false,
      "classy": false,
      "hipster": true,
      "divey": false,
      "touristy": false,
      "trendy": true,
      "upscale": false,
      "casual": true
    },
    "Parking": {
      "garage": false,
      "street": true,
      "validated": false,
      "lot": true,
      "valet": false
    },
    "Has TV": true,
    "Outdoor Seating": true,
    "Attire": "casual",
    "Alcohol": "none",
    "Waiter Service": true,
    "Accepts Credit Cards": true,
    "Good for Kids": true,
    "Good For Groups": true,
    "Price Range": 2
  },
  "type": "business"
}];

const takeOuts = yelp.filter(y => y.attributes['Take-out'] === true);
console.log(takeOuts);

如果您只使用一个对象,并且想确定它是否为Take out,请直接访问它:

var yelp = {
  "attributes": {
    "Take-out": true,
    "Drive-Thru": false,
    "Good For": {
      "dessert": false,
      "latenight": true,
      "lunch": true,
      "dinner": true,
      "brunch": true,
      "breakfast": true
    },
    "Caters": false,
    "Noise Level": "average",
    "Takes Reservations": true,
    "Delivery": false,
    "Ambience": {
      "romantic": true,
      "intimate": false,
      "classy": false,
      "hipster": true,
      "divey": false,
      "touristy": false,
      "trendy": true,
      "upscale": false,
      "casual": true
    },
    "Parking": {
      "garage": false,
      "street": true,
      "validated": false,
      "lot": true,
      "valet": false
    },
    "Has TV": true,
    "Outdoor Seating": true,
    "Attire": "casual",
    "Alcohol": "none",
    "Waiter Service": true,
    "Accepts Credit Cards": true,
    "Good for Kids": true,
    "Good For Groups": true,
    "Price Range": 2
  },
  "type": "business"
};

console.log(yelp.attributes['Take-out'] === true);

答案 1 :(得分:-1)

Take-out不是有效的标识符,在这种情况下,您需要使用[]表示法来访问属性

var yelp = {"attributes": {"Take-out": true,"Drive-Thru": false,"Good For": {"dessert": false,"latenight": true,"lunch": true,"dinner": true,"brunch": true,"breakfast": true},"Caters": false,"Noise Level": "average","Takes Reservations": true,"Delivery": false,"Ambience": {"romantic": true,"intimate": false,"classy": false,"hipster": true,"divey": false,"touristy": false,"trendy": true,"upscale": false,"casual": true},"Parking": {"garage": false,"street": true,"validated": false,"lot": true,"valet": false},"Has TV": true,"Outdoor Seating": true,"Attire": "casual","Alcohol": "none","Waiter Service": true,"Accepts Credit Cards": true,"Good for Kids": true,"Good For Groups": true,"Price Range": 2},"type": "business"}

console.log(yelp.attributes["Take-out"])

答案 2 :(得分:-1)

对象可能具有较深的层次结构,因此递归在这种情况下看起来很有用。像这样的东西。

window.Echo.private('channel1'+user_id)
  // This channel can translate only one event - "firstEvent"
  .listen("firstEvent", (mes) => {
      console.log(mes);
})
window.Echo.private('channel2'+user_id)
  // This channel can translate only one event - "secondEvent"
  .listen("secondEvent", (mes) => {
      console.log(mes);
})
// etc.