返回嵌套JSON中包含特定键的所有值
Returning all values that contain a particular key in nested JSON?
我嵌套了如下JSON:
{
"fields": {
"type": "custom",
"level1": {
"type": "custom"
},
"level2": {
"type": "custom",
"level3": {
"type": "custom"
},
"level31": {
"type": "notcustom"
}
}
}
}
我想提取所有具有密钥type
的值
我想要的输出是:
{
"fields":"custom",
"level1":"custom",
"level2":"custom"
}
我试图使用递归方法变得简单。首先检查它是否为对象。如果它是一个对象,则再次调用传递该对象的函数。
否则,请检查该键和相应的值是否存在。在结果对象中填充并返回。
var object = {
"fields": {
"type": "custom",
"level1": {
"type": "custom"
},
"level2": {
"type": "custom",
"level3": {
"type": "custom"
},
"level31": {
"type": "notcustom"
}
}
}
};
function recursiveIterator(object, needle, value, result) {
var result = result || {};
for (var key in object) {
if (object[key][needle] === value) {
result[key] = object[key][needle];
}
if (typeof object[key] === "object") {
recursiveIterator(object[key], needle, value, result);
}
}
return result;
}
document.getElementById("result").textContent = JSON.stringify(recursiveIterator(object, "type", "custom"));
<div id="result"></div>
这是一种非常通用的方法。在这种情况下,您可以传递key
(如type
),以及您想要匹配的值(在这种情况中为custom
)。
您需要一个递归函数来导航树结构。
下面的代码片段实现了这一点,并将结果添加到一个自定义字段数组中。
我没有删除自定义字段的子属性,所以如果自定义字段具有子级别属性,则该属性也包含在内。您可以删除这些子属性,但也可以修改源对象树。因此,这可以解决用Object.create
创建克隆并随后删除整个属性的问题。。。但我相信,整个代码足以让你敞开心扉,了解如何解决这个问题。
运行代码段以实时查看JSON结果!
更新
尽管您请求了一个对象作为结果(毕竟有点像字典),但我建议数组结果在这种情况下应该更好地工作,因为如果有多个"级别"属性以相同的方式调用(例如,两个level1
属性),则结果中只有最后一个可以访问,因为它可能会被覆盖
var obj = {
"fields": {
"type": "custom",
"level1": {
"type": "custom"
},
"level2": {
"type": "custom",
"level3": {
"type": "custom"
},
"level31": {
"type": "notcustom"
}
}
}
};
function getCustomFields(field, customFields) {
var customFields = typeof customFields != "undefined" ? customFields : [];
if (field["type"] == "custom") {
customFields.push(field);
}
// Object.keys gets all own object property names as an array
// in order to use filter to do a "where" the fieldName starts with
// "level" word. Once every "level" property is filtered, you execute
// an Array.forEach to execute the enclosing functin against the
// inner level giving each level field and current customFields array
// to accumulate results...
Object.keys(field).filter(function (fieldName) {
return fieldName.indexOf("level") == 0;
}).forEach(function (levelPropertyName) {
getCustomFields(field[levelPropertyName], customFields);
});
return customFields;
}
var customFields = getCustomFields(obj.fields);
document.getElementById("result").textContent = JSON.stringify(customFields);
<div id="result"></div>
相关文章:
- 访问嵌套JSON对象的键,其中键是动态的
- 从多维嵌套json数组创建下拉列表
- 带嵌套json的下划线js查找
- 返回嵌套JSON中包含特定键的所有值
- 嵌套JSON到平面HTML表
- 原型Ajax请求参数为嵌套json
- 从对象数组中动态创建嵌套json
- ExtJS 4.1-检索嵌套JSON的hasOne信息
- 更新嵌套json Angularjs中的对象
- 访问嵌套 json 对象的属性将返回未定义
- 如何解析没有键的嵌套json's的javascript
- 显示嵌套json集合的主干
- 列表中呈现的骨干嵌套json对象
- 如何使用promise来反规范化嵌套json
- 正在读取嵌套json,jquery返回undefined
- Ionic/Angular:如何在嵌套json中导航
- 如何访问AngularJs中的嵌套Json对象
- 使用Javascript查找并更新嵌套JSON数组和对象中的所有键值
- 如何获取给定表单详细信息的嵌套json对象
- 通过Javascript中的递归迭代映射嵌套JSON