我有以下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
}
]
}
]
}
我如何遍历所有empIds?
答案 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)