我有以下JSON对象
我需要根据内部数组的标题和键过滤响应,例如"ISVN8JF1E" == “key”: ISVN8JF1E
{
"1233-39CFBWYA": [],
"JMSK0DKOE": [],
"ISVN8JF1E": [
{
"delloData": "1478644629",
"ref": "75",
"dataType": "String",
"somePart": "LA",
"hello": "OUTSIDE",
"key": "ISVN8JF1E"
},
{
"delloData": "1478644629",
"ref": "75",
"dataType": "String",
"somePart": "Chicago",
"hello": "Inside",
"key": "ISVN8JF1E"
},
{
"delloData": "1478644629",
"ref": "75",
"dataType": "String",
"somePart": "Austin",
"hello": "Inside",
"key": "ABCDE"
}
],
"ISVN8JF1B": [
{
"delloData": "1478644629",
"ref": "75",
"dataType": "String",
"somePart": "LA",
"hello": "OUTSIDE",
"key": "ISVN8JF1B"
},
{
"delloData": "1478644629",
"ref": "75",
"dataType": "String",
"somePart": "Chicago",
"hello": "Inside",
"key": "ISVN8JF1C"
},
{
"delloData": "1478644629",
"ref": "75",
"dataType": "String",
"somePart": "Austin",
"hello": "Inside",
"key": "ABCDE"
}
],
"OGAESJF2EEAD3W398ZNOSA": [],
"SC9OMJF2EEAD3W398ZNOSA": []
}
以下是我的尝试:
var result = Object.keys(data).forEach(function(Id){
var updatedResult = {
[Id] : data[Id].filter(function (my) {
return my.key == Id;
})
};
totalResultArray.push(updatedResult);
});
有办法做到这一点吗?
答案 0 :(得分:0)
您可以检查给定的密钥并过滤密钥是否已设置。
var data = { "1233-39CFBWYA": [], "JMSK0DKOE": [], "ISVN8JF1E": [{ delloData: "1478644629", ref: "75", dataType: "String", somePart: "LA", hello: "OUTSIDE", key: "ISVN8JF1E" }, { delloData: "1478644629", ref: "75", dataType: "String", somePart: "Chicago", hello: "Inside", key: "ISVN8JF1E" }, { delloData: "1478644629", ref: "75", dataType: "String", somePart: "Austin", hello: "Inside", key: "ABCDE" }], "OGAESJF2EEAD3W398ZNOSA": [], "SC9OMJF2EEAD3W398ZNOSA": [] },
key = 'ISVN8JF1E',
result = (data[key] || []).filter(function (o) {
return o.key === key;
});
console.log(result);

答案 1 :(得分:0)
var blob = {
"1233-39CFBWYA": [],
"JMSK0DKOE": [],
"ISVN8JF1E": [{
"delloData": "1478644629",
"ref": "75",
"dataType": "String",
"somePart": "LA",
"hello": "OUTSIDE",
"key": "ISVN8JF1E"
}, {
"delloData ": "1478644629 ",
"ref ": "75 ",
"dataType ": "String ",
"somePart ": "Chicago ",
"hello ": "Inside",
"key": "ISVN8JF1E"
}, {
"delloData ": "1478644629 ",
"ref ": "75 ",
"dataType ": "String ",
"somePart ": "Austin ",
"hello ": "Inside",
"key": "ABCDE"
}],
"OGAESJF2EEAD3W398ZNOSA ": [],
"SC9OMJF2EEAD3W398ZNOSA ": []
};
var match_key = "ISVN8JF1E";
var filtered = blob["ISVN8JF1E"].filter(function(val) {
return val.key == match_key;
});
console.log(filtered);