如何从数组中删除对象?我想删除,其中包括名称的对象Kristian
从someArray
。例如:
someArray = [{name:"Kristian", lines:"2,5,10"},
{name:"John", lines:"1,19,26,96"}];
我要实现:
someArray = [{name:"John", lines:"1,19,26,96"}];
如何从数组中删除对象?我想删除,其中包括名称的对象Kristian
从someArray
。例如:
someArray = [{name:"Kristian", lines:"2,5,10"},
{name:"John", lines:"1,19,26,96"}];
我要实现:
someArray = [{name:"John", lines:"1,19,26,96"}];
This Concepts using Kendo Grid
var grid = $("#addNewAllergies").data("kendoGrid");
var selectedItem = SelectedCheckBoxList;
for (var i = 0; i < selectedItem.length; i++) {
if(selectedItem[i].boolKendoValue==true)
{
selectedItem.length= 0;
}
}
Returns only objects from the array whose property name
is not "Kristian"
var noKristianArray = $.grep(someArray, function (el) { return el.name!= "Kristian"; });
var someArray = [
{name:"Kristian", lines:"2,5,10"},
{name:"John", lines:"1,19,26,96"},
{name:"Kristian", lines:"2,58,160"},
{name:"Felix", lines:"1,19,26,96"}
];
var noKristianArray = $.grep(someArray, function (el) { return el.name!= "Kristian"; });
console.log(noKristianArray);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
You could also use some
:
someArray = [{name:"Kristian", lines:"2,5,10"},
{name:"John", lines:"1,19,26,96"}];
someArray.some(item => {
if(item.name === "Kristian") // Case sensitive, will only remove first instance
someArray.splice(someArray.indexOf(item),1)
})
If you want to remove all occurrences of a given object (based on some condition) then use the javascript splice method inside a for the loop.
Since removing an object would affect the array length, make sure to decrement the counter one step, so that length check remains intact.
var objArr=[{Name:"Alex", Age:62},
{Name:"Robert", Age:18},
{Name:"Prince", Age:28},
{Name:"Cesar", Age:38},
{Name:"Sam", Age:42},
{Name:"David", Age:52}
];
for(var i = 0;i < objArr.length; i ++)
{
if(objArr[i].Age > 20)
{
objArr.splice(i, 1);
i--; //re-adjust the counter.
}
}
The above code snippet removes all objects with age greater than 20.
具有ES 6箭头功能
let someArray = [
{name:"Kristian", lines:"2,5,10"},
{name:"John", lines:"1,19,26,96"}
];
let arrayToRemove={name:"Kristian", lines:"2,5,10"};
someArray=someArray.filter((e)=>e.name !=arrayToRemove.name && e.lines!= arrayToRemove.lines)
There seems to be an error in your array syntax so assuming you mean an array as opposed to an object, Array.splice is your friend here:
someArray = [{name:"Kristian", lines:"2,5,10"}, {name:"John", lines:"1,19,26,96"}];
someArray.splice(1,1)
This is what I use.
Array.prototype.delete = function(pos){
this[pos] = undefined;
var len = this.length - 1;
for(var a = pos;a < this.length - 1;a++){
this[a] = this[a+1];
}
this.pop();
}
Then it is as simple as saying
var myArray = [1,2,3,4,5,6,7,8,9];
myArray.delete(3);
Replace any number in place of three. After the expected output should be:
console.log(myArray); //Expected output 1,2,3,5,6,7,8,9
This answer
for (var i =0; i < someArray.length; i++)
if (someArray[i].name === "Kristian") {
someArray.splice(i,1);
}
is not working for multiple records fulfilling the condition. If you have two such consecutive records, only the first one is removed, and the other one skipped. You have to use:
for (var i = someArray.length - 1; i>= 0; i--)
...
instead .
You can use map function also.
someArray = [{name:"Kristian", lines:"2,5,10"},{name:"John",lines:"1,19,26,96"}];
newArray=[];
someArray.map(function(obj, index){
if(obj.name !== "Kristian"){
newArray.push(obj);
}
});
someArray = newArray;
console.log(someArray);
这是一个对我有用的函数:
function removeFromArray(array, value) {
var idx = array.indexOf(value);
if (idx !== -1) {
array.splice(idx, 1);
}
return array;
}
在数组上使用拼接功能。指定起始元素的位置和要删除的子序列的长度。
someArray.splice(pos, 1);
我制作了一个动态函数,将对象Array,Key和value取出,并在删除所需对象后返回相同的数组:
function removeFunction (myObjects,prop,valu)
{
return myObjects.filter(function (val) {
return val[prop] !== valu;
});
}
完整示例:DEMO
var obj = {
"results": [
{
"id": "460",
"name": "Widget 1",
"loc": "Shed"
}, {
"id": "461",
"name": "Widget 2",
"loc": "Kitchen"
}, {
"id": "462",
"name": "Widget 3",
"loc": "bath"
}
]
};
function removeFunction (myObjects,prop,valu)
{
return myObjects.filter(function (val) {
return val[prop] !== valu;
});
}
console.log(removeFunction(obj.results,"id","460"));
所示的“数组”是无效的JavaScript语法。圆括号{}
用于具有属性名称/值对的对象,但方括号[]
用于数组-像这样:
someArray = [{name:"Kristian", lines:"2,5,10"}, {name:"John", lines:"1,19,26,96"}];
在这种情况下,您可以使用该.splice()
方法删除项目。要删除第一项(索引0),请说:
someArray.splice(0,1);
// someArray = [{name:"John", lines:"1,19,26,96"}];
如果您不知道索引,但想在数组中搜索以找到名称为“ Kristian”的项,则可以删除该项:
for (var i =0; i < someArray.length; i++)
if (someArray[i].name === "Kristian") {
someArray.splice(i,1);
break;
}
EDIT: I just noticed your question is tagged with "jQuery", so you could try the $.grep()
method:
someArray = $.grep(someArray,
function(o,i) { return o.name === "Kristian"; },
true);
干净的解决方案是使用Array.filter
:
var filtered = someArray.filter(function(el) { return el.Name != "Kristian"; });
这样做的问题是它不能在IE <9上运行。但是,您可以包括来自Java库的代码(例如underscore.js),该代码可为任何浏览器实现此功能。
If you wanna access and remove object of an array simply you can try something like this.