Javascript JSON过滤属性
过滤并删除JSON嵌套键的最佳方法是什么?例如:Javascript JSON过滤属性,javascript,jquery,ajax,json,Javascript,Jquery,Ajax,Json,过滤并删除JSON嵌套键的最佳方法是什么?例如: { "id" : "1", "key1" : "val1", "key2" : "val2", "name" : "someone", "age" : 39, "data" : [ { "id" : "1234", "key1" : "val1", "key2" : "val2", "name" : "someone", "age" :
{ "id" : "1",
"key1" : "val1",
"key2" : "val2",
"name" : "someone",
"age" : 39,
"data" : [
{ "id" : "1234",
"key1" : "val1",
"key2" : "val2",
"name" : "someone",
"age" : 39
},
{ "id" : "1234",
"key1" : "val1",
"key2" : "val2",
"name" : "someone",
"age" : 39
}
]
}
通过递归删除所有key1
和key2
项来获取以下JSON:
{ "id" : "1",
"name" : "someone",
"age" : 39,
"data" : [
{ "id" : "1234",
"name" : "someone",
"age" : 39
},
{ "id" : "1234",
"name" : "someone",
"age" : 39
}
]
}
谢谢。像这样的方法应该可以:
function deleteRecursive(data, key) {
for(var property in data) {
if(data.hasOwnProperty(property)) {
if(property == key) {
delete data[key];
}
else {
if(typeof data[property] === "object") {
deleteRecursive(data[property], key);
}
}
}
}
}
function objWithoutPropsIDontLike(obj, propsIDontLike) {
// check to make sure the given parameter is an object
if(typeof obj == "object" && obj !== null) { // typeof null gives "object" ಠ_ಠ
// for every property name... (see note on Object.keys() and
// Array.forEach() below)
obj.keys().forEach(function(prop) {
// Test if the property name is one of the ones you don't like
// (Array.indexOf() returns -1 if the item isn't found in the array).
if(propsIDontLike.indexOf(prop) >= 0) {
// if it is, nuke it
delete obj[prop];
} else if(obj[prop]) {
// if it isn't, recursively filter it
obj[prop] = filterPropsIDontLike(obj[prop], propsIDontLike);
}
});
}
// There is no else { ... }; if the thing given for "obj" isn't an object
// just return it as-is.
return obj;
}
var propsIDontLike = [ 'key1', 'key2' ];
people = objWithoutPropsIDontLike(people, propsIDontLike);
您的问题包含您的答案:递归 基本情况是“基本”JSON类型:字符串和数字。这些都没有改变。对于数组,将操作应用于数组的每个元素,并返回一个新数组 有趣的例子是对象。这里,对于每个键-值对,您将操作应用于每个值(但忽略那些其键是您要“删除”的键的值),并将它们写入一个新对象 作为一个(即兴)示例,使用jQuery:
var removeKey(object, key){
if(typeof(object)==='number' || typeof(object)==='string'){
return object;
}
else if(typeof(object)==='object'){
var newObject = {};
$.each(object, function(k, value) {
if(k!==key){
newObject[k] = removeKey(value, key);
}
});
return newObject;
}
else {
// Oh dear, that wasn't really JSON!
}
};
如果要删除多个关键点,请根据需要调整递归案例中的第二个参数和条件
注意这是一种非破坏性的,可能是也可能不是您所需要的;另一个答案(Vivin Paliath)有一个破坏性的版本。假设这是一个对象的JSON,比如说,
people
,类似的东西应该可以工作:
function deleteRecursive(data, key) {
for(var property in data) {
if(data.hasOwnProperty(property)) {
if(property == key) {
delete data[key];
}
else {
if(typeof data[property] === "object") {
deleteRecursive(data[property], key);
}
}
}
}
}
function objWithoutPropsIDontLike(obj, propsIDontLike) {
// check to make sure the given parameter is an object
if(typeof obj == "object" && obj !== null) { // typeof null gives "object" ಠ_ಠ
// for every property name... (see note on Object.keys() and
// Array.forEach() below)
obj.keys().forEach(function(prop) {
// Test if the property name is one of the ones you don't like
// (Array.indexOf() returns -1 if the item isn't found in the array).
if(propsIDontLike.indexOf(prop) >= 0) {
// if it is, nuke it
delete obj[prop];
} else if(obj[prop]) {
// if it isn't, recursively filter it
obj[prop] = filterPropsIDontLike(obj[prop], propsIDontLike);
}
});
}
// There is no else { ... }; if the thing given for "obj" isn't an object
// just return it as-is.
return obj;
}
var propsIDontLike = [ 'key1', 'key2' ];
people = objWithoutPropsIDontLike(people, propsIDontLike);
注:
Object.keys()
和Array.forEach()
在Internet Explorer<9中不可用。令人高兴的是,MDC为以下两种提供了工作多边形填充:,。检查花括号,其中有语法错误