我正在将Angular JS与下划线JS一起使用
我有一个上面显示的JSON,从上面的JSON中,我试图提取所有array类型的Key。
[{
"_id": "591c2748f6d215203ee12241",
"LineSystemOid": "{00033457-0000-0000-F306-D0D1B64F1D04}",
"LineName": "P-10148",
"Filter1": [{
"Reason_1": 1,
"ReasonShrtStrngVal": "Working",
"ReasonLngStrngVal": "Working"
}],
"Filter2": [{
"Status_1": 1,
"StatusShrtStrngVal": "Working",
"StatusLngStrngVal": "Working"
}],
"DateCreated": "2012-05-18 22:54:18.637",
"DateLastModified": "2014-05-15 22:43:27.533",
"Filter3": [{
"CorrelationStatus_1": null,
"CorrelationStatusShrtStrngVal": null,
"CorrelationStatusLngStrngVal": null
}]
}]
表示我需要的输出是
Filter1, Filter2 and Filter3
我尝试了如下,但是我得到的输出是
var allkeys = _.keys(reasons[0]);
console.log(JSON.stringify(allkeys));
输出:
["_id","LineSystemOid","LineName","Filter1","Filter2","DateCreated","DateLastModified"]
我的小提琴
http://jsfiddle.net/vEDcx/10/
最佳答案
我的解决方案-http://jsfiddle.net/d226aco7/
var reasons = [{
"_id": "591c2748f6d215203ee12241",
"LineSystemOid": "{00033457-0000-0000-F306-D0D1B64F1D04}",
"LineName": "P-10148",
"Filter1": [{
"Reason_1": 1,
"ReasonShrtStrngVal": "Working",
"ReasonLngStrngVal": "Working"
}],
"Filter2": [{
"Status_1": 1,
"StatusShrtStrngVal": "Working",
"StatusLngStrngVal": "Working"
}],
"DateCreated": "2012-05-18 22:54:18.637",
"DateLastModified": "2014-05-15 22:43:27.533",
"Filter2": [{
"CorrelationStatus_1": null,
"CorrelationStatusShrtStrngVal": null,
"CorrelationStatusLngStrngVal": null
}]
}]
var allkeys = [];
_.forEach(reasons[0], function(value, key) {
if (_.isArray(value)) {
allkeys.push(key);
}
});
console.log(JSON.stringify(allkeys));
关于jquery - 如何仅拾取Array类型的键,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44026220/