What is the best way to check if an object has items with negative values using javascript
or ES6
, if that is the case remove it from the object?
foo = { 0: 0, 1: -1, 2: 2, 3: -1}
result should be the one below
foo = { 0: 0, 1: 2}
Sorry, i am ing from a python
background but i would love to hear from you.
What is the best way to check if an object has items with negative values using javascript
or ES6
, if that is the case remove it from the object?
foo = { 0: 0, 1: -1, 2: 2, 3: -1}
result should be the one below
foo = { 0: 0, 1: 2}
Sorry, i am ing from a python
background but i would love to hear from you.
- 1 Do you want to mutate the object or create a new one? – trincot Commented Aug 24, 2020 at 17:42
- @trincot yes! i am fine with that. – John D Commented Aug 24, 2020 at 17:43
- 3 It was an or-question...? You mean "yes mutate" or "yes new"? – trincot Commented Aug 24, 2020 at 17:43
-
2
From the looks of it (not keeping the key value paring and keys always being consecutive numbers that start at
0
). You should probably use an array. – Titus Commented Aug 24, 2020 at 17:44 -
@trincot my bad.
create a new one
would be fine – John D Commented Aug 24, 2020 at 17:45
6 Answers
Reset to default 4 +50You could filter the values and get a new object.
Assumptions:
- Object contains only index like keys (32 bit positive integer values).
- Keys starts from zero to n.
- The order should remain constant.
- The new object has now keys from zero to m where
m <= n
, depending of the count of the positive values.
The solution takes the values of the object in the standard order of ECMA-262 (Does JavaScript guarantee object property order?), filter this array and assigns the array to an object.
The result is an object with key and values from the filtered array.
const
data = { 0: 0, 1: -1, 2: 2, 3: -1 },
result = Object.assign({}, Object.values(data).filter(v => v >= 0));
console.log(result);
You can use .filter
to remove elements with negative values:
foo = { 0: 0, 1: -1, 2: 2, 3: -1}
let positives = Object.entries(foo).filter(e => e[1]>=0);
foo = {};
positives.forEach(([key, value], index) => {
foo[index] = value;
});
console.log(foo);
If you want to modify the existing object rather than make a copy, you can do this:
let foo = { 0: 0, 1: -1, 2: 2, 3: -1}
for (let [key, value] of Object.entries(foo)) {
if (typeof value === "number" && value < 0) {
delete foo[key];
}
}
console.log(foo);
Note: I added the typeof value === "number"
as a type-safety check so you make sure you're paring a numeric value in case there are other types of properties.
Or, in a reusable function that can be used with a user-supplied condition:
// callback passed (key, value) and
// return true to keep, false to remove the property
// function returns original object with desired properties removed
function filterProperties(obj, callback) {
for (let [key, value] of Object.entries(obj)) {
if (callback(key, value) === false) {
delete obj[key];
}
}
return obj;
}
let foo = { 0: 0, 1: -1, 2: 2, 3: -1}
filterProperties(foo, (key, value) => !(typeof value === "number" && value < 0));
console.log(foo);
If you want to create a new object
const fooWithoutNegativeValues = Object.entries(foo).reduce((acc, [key, value]) => value >= 0 ? {...acc, [key]: value} : acc)
To mutate its not different
const foo = { 0: 0, 1: -1, 2: 2, 3: -1}
const result = Object.keys(foo).reduce((acc, curr) => {
if(foo[curr] >= 0) {
acc[curr] = foo[curr]
}
return acc;
}, {})
console.log(result)
This code will only modify the object to delete negatives.
foo = { 0: 0, 1: -1, 2: 2, 3: -1}
Object.keys(foo).map((key) => {
if(foo[key]<0){
delete foo[key]
}
})
console.log(foo);
For a short explanation, Object.keys(foo) will turn the object into an array based on the key names. Then .map() loops through each key in the array, and if the condition passes, it will delete the variable.