【问题标题】:How to get values in a JSON array object如何获取 JSON 数组对象中的值
【发布时间】:2021-03-18 02:24:40
【问题描述】:

我是 javascript 用法的新手。 我需要以下 JSON 对象消息。

{
  "Rep": {
    "out": [
      {
        "first": "abc",
        "second": "fd",
        "state": "none",
        "badge": "NON",
        "res": 0
      },
      {
        "first": "xyz",
        "second": "5f7209",
        "state": 15,
        "badge": "OH",
        "res": 0
      },
      {
        "first": "def",
        "second": "b5fd",
        "badge": "Pen",
        "state": 3,
        "res": 0
      },
      {
        "first": "aa",
        "second": "5e",
        "badge": "Com",
        "state": 1,
        "res": 0
      }
    ]
  }
}

要求是我必须遍历并从对象中获取“第一个”键标记值,并检查该值是否等于某个值(这里是“第一个”== 'aa')。 如果存在,我必须从相应的对象中获取 'badge' 的值。

示例 - 来自消息

1)我必须检查“first == aa”是否存在,然后我应该检查对象'res'的值是否为0,然后取badge的值,这里是“com”。

2)我必须检查“first == def”是否存在,然后我应该检查对象'res'的值是否为0,然后取badge的值,这里是“Pen”。

以类似的方式,我必须遍历每个块并检查“第一个”键值,然后获取相应的徽章值。

注意:我们不确定数组中有多少对象块。

【问题讨论】:

  • 不确定问题出在哪里......听起来你需要一个循环
  • 为此使用数组过滤器。例如let result = array['Rep']['out'].filter((v) => v.first === 'abc'); if( result.length) console.log( result[0].badge );
  • 或使用数组 find() 函数查找第一次出现
  • 你在这个位置缺少双引号 "second": "5e,

标签: javascript arrays json object


【解决方案1】:
object={
  "Rep": {
    "out": [
      {
        "first": "abc",
        "second": "fd",
        "state": "none",
        "badge": "NON",
        "res": 0
      },
      {
        "first": "xyz",
        "second": "5f7209",
        "state": 15,
        "badge": "OH",
        "res": 0
      },
      {
        "first": "def",
        "second": "b5fd",
        "badge": "Pen",
        "state": 3,
        "res": 0
      },
      {
        "first": "aa",
        "second": "5e",
        "badge": "Com",
        "state": 1,
        "res": 0
      }
    ]
  }
}

in_list=object.Rep.out
in_list.forEach((arr)=>{
  if (arr.first=="def"){
    console.log(arr.badge);
  }
});

您可以通过 JSON.Key 读取 JSON 中的值,并且可以使用 forEach() 遍历 Array。然后您可以使用条件来获取相关的徽章值。希望这会有所帮助!

【讨论】:

    【解决方案2】:

    假设您将 json 消息存储在一个名为 jsonMessage 的变量中,如下所示:

    const jsonMessage = {
      "Rep": {
        "out": [
          {
            "first": "abc",
            "second": "fd",
            "state": "none",
            "badge": "NON",
            "res": 0
          },
          {
            "first": "xyz",
            "second": "5f7209",
            "state": 15,
            "badge": "OH",
            "res": 0
          },
          {
            "first": "def",
            "second": "b5fd",
            "badge": "Pen",
            "state": 3,
            "res": 0
          },
          {
            "first": "aa",
            "second": "5e",
            "badge": "Com",
            "state": 1,
            "res": 0
          }
        ]
      }
    }
    
    // Now you can refers "out" array directly, its called destructuring (google it, its pretty usefull)
    const { Rep: { out } } = jsonMessage
    
    // Filter objects that fulfill conditions
    const validObjects = out.filter( obj => {
        return (obj.first === "aa" && obj.res === 0) || (obj.first === "def" && obj.res === 0)
    })
    
    // Now you have an array with valid objects, for each object in array you can recover badge value
    validObjects.forEach( obj => {
        console.log(obj.badge)
    })
    
    

    【讨论】:

      【解决方案3】:

      你可以使用 ma​​p() 方法来玩你想要的条件。

      const jsonMessage = {
          "Rep": {
              "out": [
              {
                  "first": "abc",
                  "second": "fd",
                  "state": "none",
                  "badge": "NON",
                  "res": 0
              },
              {
                  "first": "xyz",
                  "second": "5f7209",
                  "state": 15,
                  "badge": "OH",
                  "res": 0
              },
              {
                  "first": "def",
                  "second": "b5fd",
                  "badge": "Pen",
                  "state": 3,
                  "res": 0
              },
              {
                  "first": "aa",
                  "second": "5e",
                  "badge": "Com",
                  "state": 1,
                  "res": 0
              }]
          }
      }
      
      jsonMessage.Rep.out.map(function(params) {
          if((params.first=="aa" && params.res ==0) || (params.first === "def" && params.res == 0)) {
              console.log(params.badge);
          } 
      });

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-12-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多