如何遍历整个JSON并获取所有的Empid?

时间:2018-09-14 07:01:35

标签: jquery json

我有以下JSON

{
  "name": "Employee Fields",
  "id": "Employee",
  "rows": [
    {
      "data": [
        {
          "emp": "111",
          "label": "Name",
          "disabled": false
        },
        {
          "emp": "112",
          "label": "Name",
          "disabled": false
        }
      ]
    },
    {
      "data": [
        {
          "emp": "113",
          "label": "Name",
          "disabled": false
        },
        {
          "emp": "114",
          "label": "Name",
          "disabled": false
        }
      ]
    },
    {
      "data": [
        {
          "emp": "115",
          "label": "Name",
          "disabled": false
        },
        {
          "emp": "116",
          "label": "Name",
          "disabled": false
        }
      ]
    }
  ]
}

enter image description here

我如何遍历所有empIds?

https://jsfiddle.net/o2gxgz9r/70859/

3 个答案:

答案 0 :(得分:2)

$(document).ready(function() {
  
  var fields = 
  {
    "name": "Employee Fields",
    "id": "Employee",
    "rows": [
      {
        "data": [
          {
            "emp": "111",
            "label": "Name",
            "disabled": false
          },
          {
            "emp": "112",
            "label": "Name",
            "disabled": false
          }
        ]
      },
      {
        "data": [
          {
            "emp": "113",
            "label": "Name",
            "disabled": false
          },
          {
            "emp": "114",
            "label": "Name",
            "disabled": false
          }
        ]
      },
      {
        "data": [
          {
            "emp": "115",
            "label": "Name",
            "disabled": false
          },
          {
            "emp": "116",
            "label": "Name",
            "disabled": false
          }
        ]
      }
    ]
  };


  $.each(fields.rows, function(index, row) {
    $.each(row.data, function(index, emp) {
      console.log("Label: ", emp.label);
      console.log("Disabled: ", emp.disabled);
      console.log("ID: ", emp.emp);
      console.log("----");
    });
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

答案 1 :(得分:0)

    var foo = {
      "name": "Employee Fields",
      "id": "Employee",
      "rows": [
        {
          "data": [
            {
              "emp": "111",
              "label": "Name",
              "disabled": false
            },
            {
              "emp": "112",
              "label": "Name",
              "disabled": false
            }
          ]
        },
        {
          "data": [
            {
              "emp": "113",
              "label": "Name",
              "disabled": false
            },
            {
              "emp": "114",
              "label": "Name",
              "disabled": false
            }
          ]
        },
        {
          "data": [
            {
              "emp": "115",
              "label": "Name",
              "disabled": false
            },
            {
              "emp": "116",
              "label": "Name",
              "disabled": false
            }
          ]
        }
      ]
    }
    
    foo.rows.forEach(row => {
    	row.data.forEach(e => {
    		console.log(e.emp)
    	})
    })

答案 2 :(得分:0)

您可以使用map()concat()

var data ={"name":"Employee Fields","id":"Employee","rows":[{"data":[{"emp":"111","label":"Name","disabled":false},{"emp":"112","label":"Name","disabled":false}]},{"data":[{"emp":"113","label":"Name","disabled":false},{"emp":"114","label":"Name","disabled":false}]},{"data":[{"emp":"115","label":"Name","disabled":false},{"emp":"116","label":"Name","disabled":false}]}]}
let result =[].concat.apply([],data.rows.map(({data})=>data.map(({emp})=>emp)))

console.log(result)

还要使用reduce()map()concat()

var data ={"name":"Employee Fields","id":"Employee","rows":[{"data":[{"emp":"111","label":"Name","disabled":false},{"emp":"112","label":"Name","disabled":false}]},{"data":[{"emp":"113","label":"Name","disabled":false},{"emp":"114","label":"Name","disabled":false}]},{"data":[{"emp":"115","label":"Name","disabled":false},{"emp":"116","label":"Name","disabled":false}]}]}
let result = data.rows.reduce((r,{data})=>r.concat(data.map(({emp})=>emp)),[]);

console.log(result)