Javascript 按值删除 JSON 条目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14400609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove JSON entry by value
提问by André Figueira
Possible Duplicate:
Delete from array in javascript
可能的重复:
从 javascript 中的数组中删除
I have the following JSON object:
我有以下 JSON 对象:
[id:84,id:92,id:123,id:2353]
How would I go about removing the item which the value is "123" using javascript?
我将如何使用javascript删除值为“123”的项目?
or if I formatted the json as
或者如果我将 json 格式化为
[84, 92, 123, 2353]
How would it be removed in this case?
在这种情况下如何将其删除?
回答by nekman
Assume you have this:
假设你有这个:
var items = [{ id: 84 }, { id: 92 }, { id: 123 }, { id: 2353 }];
var filtered = items.filter(function(item) {
return item.id !== 123;
});
//filtered => [{ id: 84 }, { id: 92 }, { id: 2353 }]
回答by Magus
Supposing you actually have an object from a json in the jsonvariable
假设您实际上在json变量中有一个来自 json 的对象
for (key in json) {
if (json.hasOwnProperty(key) && json[key] == 123) {
delete json[key];
}
}
回答by techfoobar
Shorter alternative would be:
更短的选择是:
var newArr = [{id:84}, {id:92}, {id:123}, {id:2353}].filter(function(a) {
return a.id != 123;
});
If you have this:
如果你有这个:
var arr = [{id:84}, {id:92}, {id:123}, {id:2353}]
var arr = [{id:84}, {id:92}, {id:123}, {id:2353}]
To remove the item with value 123, you can do:
要删除值为 123 的项目,您可以执行以下操作:
for(var i = 0; i < arr.length; i++) {
if(arr[i].id == 123) {
arr.splice(i, 1);
break;
}
}
回答by Vismari
You can use splice function, like this:
您可以使用 splice 功能,如下所示:
var data = [{id:84}, {id:92}, {id:123}, {id:2353}];
function remove(){
for(var i = 0, max = data.length; i < max; i++) {
var a = data[i];
if(a.id === 123) {
data.splice(i, 1);
break;
}
}
}
remove();
回答by Explosion Pills
Seems like you want to avoid a loop. Assuming it's available, you can use .filter:
似乎您想避免循环。假设它可用,您可以使用.filter:
[{id:84},{id:92},{id:123},{id:2353}]
.filter(function (elem) { return elem.id !== 123; });
This technically does do a loop, but at least you don't have to look at it.
这在技术上确实做了一个循环,但至少你不必看它。
回答by Bart
Assuming your "json" is really an array, like [84, 92, 123, 2353]:
假设您的“json”确实是一个数组,例如 [84, 92, 123, 2353]:
var myString = "[84, 92, 123, 2353]";
var myArray = JSON.parse(myString);
var index = myArray.indexOf(123); // whatever value you are looking for
myArray.splice(index, 1);
回答by moddom
function removeClass(obj, cls) {
var classes = obj.className.split(' ');
for(i=0; i<classes.length; i++) {
if (classes[i] == cls) {
classes.splice(i, 1);
i--; // (*)
}
}
obj.className = classes.join(' ');
}
var obj = { className: 'open menu menu' }
removeClass(obj, 'menu')
alert(obj.className)
回答by Lloyd
Assuming I'm understanding your question and comments correctly you can do something like this:
假设我正确理解您的问题和评论,您可以执行以下操作:
var old_array = [{id: 84},...];
var new_array = [];
for(var i = 0, len = old_array.length; i++) {
if (old_array[i].id != 123) new_array.push(old_array[i]);
}
回答by Paul S.
What you have currently is not JSONso I'll give you some different options.
您目前拥有的不是JSON,所以我会给您一些不同的选择。
If you have an Arrayarr = [84,92,123,2353]then
如果你有一个数组,arr = [84,92,123,2353]那么
arr = arr.filter(function (x) {return x !== 123;}); // all occurrences
// OR
arr.splice(arr.indexOf(123), 1); // first occurrence only
If you have an Objectobj = {"84": a, "92": b, "123": c, "2353": d}, ato dsome expressions, then
如果你有一个Objectobj = {"84": a, "92": b, "123": c, "2353": d},a对于d某些表达式,那么
delete obj['123']; // obj now {"84": a, "92": b, "2353": d}
回答by Norguard
1) JSON is a string, not an array or an object.
1) JSON 是字符串,而不是数组或对象。
var json = "[1,2,3]";
2) Valid JSON NEEDS to be valid JS
2) 有效的 JSON需要是有效的 JS
var myJSObj = { 1,2,3 }, // broken
myJSArr = [ name : 1, name2 : 2 ]; // broken
3) If you have a JS Array, you can remove an element by using [].splice
3)如果你有一个JS数组,你可以使用 [].splice
var arr = [ 1, 2, 3, 4 ],
i = 0, l = arr.length,
test = 4;
for (; i < l; i += 1) {
if (arr[i] === test) { arr.splice(i, 1); } // remove 1 starting at i
}
4) If you have an object with named keys, you can use delete
4)如果你有一个带有命名键的对象,你可以使用 delete
var obj = { val : 1 };
delete obj.val;

