I have an objects array like this:
Array ( [945] => member Object ( [id] => 13317 [name] => Test 999 [last_name] => Test 999 ) [54] => member Object ( [id] => 13316 [name] => Manuel [last_name] => Maria parra ) [654] => member Object ( [id] => 13315 [name] => Byron [last_name] => Castillo ) [656] => member Object ( [id] => 13314 [name] => Cesar [last_name] => Vasquez ) )
I need to remove one of these objects according to an attribute value.
For example, I want to remove from the array the object id 13316.
For getting all of the keys of an Object you can use Object. keys() . Object. keys() takes an object as an argument and returns an array of all the keys.
Using the indexOf() Method JavaScript's indexOf() method will return the index of the first instance of an element in the array. If the element does not exist then, -1 is returned.
To check if an element exists in an array in React: Use the includes() method to check if a primitive exists in an array. Use the some() method to check if an object exists in an array.
Here is the functional approach:
$neededObjects = array_filter( $objects, function ($e) use ($idToFilter) { return $e->id != $idToFilter; } );
function filter_by_key($array, $member, $value) { $filtered = array(); foreach($array as $k => $v) { if($v->$member != $value) $filtered[$k] = $v; } return $filtered; } $array = ... $array = filter_by_key($array, 'id', 13316);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With