Find and remove objects in an array based on a key value in JavaScript
I can grep the array for the id, but how can I delete the entire object where id == 88
Simply filter by the opposite predicate:
var data = $.grep(data, function(e){
return e.id != id;
});
Remove Objects with same key values from array of objects
Array.prototype.filter
is what you are looking for, check this out for more information.
const arr = [{name: "a", value: "b"}, {name: "a", value: "d"}, {name: "f", value: "r"}, {name: "g", value: "q"}];
console.log(arr.filter((item) => {
return item.name !== "a";
}));
Delete object from an array if the key value match
Using Array#findIndex
:
const removeElemWithIdAndValue = (arr = [], targetId, targetValue) => {
const index = arr.findIndex(({ id, value }) => id === targetId && value === targetValue);
if(index >= 0) arr.splice(index, 1);
}
const arr = [ {id: 145, value: '$ 1.024.100'}, {id: 146, value: '$ 679.200'}, {id: 147, value: '$ 679.200'} ];
removeElemWithIdAndValue(arr, 1461, '$ 679.200');
console.log(arr);
Remove array element based on object property
One possibility:
myArray = myArray.filter(function( obj ) {
return obj.field !== 'money';
});
Please note that filter
creates a new array. Any other variables referring to the original array would not get the filtered data although you update your original variable myArray
with the new reference. Use with caution.
Most efficient way to remove objects from array based on key value objects in another array
You can use Array#filter
, as suggested by @Barmar, in combination with Array#every
as follows:
const excludes = [{key: "color", value: "Red"}, {key: "age", value:12}, {key:"score", value: 75}],
items = [{color: "Red", score: 30, age: 12}, {color: "Blue", score: 100, age: 20}, {color: "Red", score: 75, age: 30}],
output = items.filter(
item => excludes.every(
({key,value}) => item[key] !== value
)
);
console.log( output );
//OUTPUT: [{color: "Blue", score: 100, age: 20}]
How to remove specific key and value from array object in javascript?
You can use rest parameter. which will come handy when you have lot's
of keys which you want to keep and removing only few of them.
const arrayData= [{index: 0,is_required: true,name: "vmvdnksl",type: "LONG_TEXT"},{index: 1,is_required: true,name: "dsvnlk",type: "MULTIPLE_SELECTORS"}];
const result = arrayData.map(({type,index,...rest}) => ({...rest}));
console.log(result);
Angular remove object from array search for key and value
Works if key is "id", and if you are sure there won't be repeated values:
var data = [ { "id": "02" }, { "id": "03" } ];
function removeVal(value) {
var removeIndex = -1;
data.forEach((obj, index) => {
if (obj.id === value) {
removeIndex = index;
}
});
data.splice(removeIndex, 1);
console.log(data);
}
removeVal("02");
How to remove a simple specific key value pair from all objects inside an array
objArr.forEach(a => delete a.theId);
Docs for delete operator: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete
Remove all objects from array with a specific key value
filter()
is your solution. It takes a callback as an argument that decides if the element should be kept in the array and returns the filtered array.
function remove(arr, pid) { return arr.filter(e => e.pid !== pid);}
let arr = [{ pid: 1 }, { pid: 2 }];console.log("Removed pid:1", remove(arr, 1));console.log("Removed pid:2", remove(arr, 2));console.log("Removed pid:3", remove(arr, 3));
let yourArr = [{ "id": 1523898500862, "amm": 1, "type": "t", "name": "bluecheese", "pid": 1523898494726, "cost": 0.5}, { "id": 1523898501937, "amm": 1, "type": "t", "name": "edam", "pid": 1523898494726, "cost": 0.5}, { "id": 1523898505766, "amm": 1, "type": "t", "name": "mozzarella", "pid": 1523898494726, "cost": 1}];console.log("Removed from your array", remove(yourArr, 1523898494726));
Related Topics
Firebase: Setting Additional User Properties
Why Does Array.Prototype.Push Return the New Length Instead of Something More Useful
How to Escape JavaScript in Jsp
How to Create a Jquery Plugin with Methods
Invoking a Function Without Parentheses
Best Way to Add Page Specific JavaScript in a Rails 3 App
Smooth Scroll to Div Id Jquery
React Context VS React Redux, When Should I Use Each One
Do Something If Screen Width Is Less Than 960 Px
How to Process Each Letter of Text Using JavaScript
Reactjs: Maximum Update Depth Exceeded Error
How to Strip All Punctuation from a String in JavaScript Using Regex
Call Multiple Functions Onclick Reactjs
Changing an Element's Id with Jquery
$(Document).Ready(Function(){}); VS Script at the Bottom of Page
How to Delay a Function Call for 5 Seconds
Precise Financial Calculation in JavaScript. What Are the Gotchas