0

I have a problems, I don't work with keys in general, but I got this problem now, I have an object like this :

let A = {
            'a': null;
            'b': null,
            'c': null,
            'd': { isOk : 'Yes' },
            'e': { isOk : 'No' },
            'f': { label : 'field'}
       }

I want to filter this object and throw the nulls, I want a result like this :

 {
      'd': { isOk : 'Yes' },
      'e': { isOk : 'No' },
      'f': { label : 'field'}
 }

I can't verify one by one, because keys are dynamic, I can't use map or for on an object

I am trying to just transform this into array so I can use filter method after, but did not make it :

console.log(
    pipe(
      toPairs,
      map(
        ([id, props]) => ({
          id,
          ...props,
        }),
        A,
      ),
    ),
  );

Any help ?

TaouBen
  • 1,165
  • 1
  • 15
  • 41
  • Does this answer your question? [Remove blank attributes from an Object in Javascript](https://stackoverflow.com/questions/286141/remove-blank-attributes-from-an-object-in-javascript) – halilcakar Jun 26 '20 at 17:29
  • No, it does not, I am looking for a solution with ramda library as mentionned in the tags – TaouBen Jun 26 '20 at 17:44
  • :) you are right. Didn't realize that it needs to be done with `ramda.js` – halilcakar Jun 26 '20 at 17:46

4 Answers4

3

Use R.reject() with R.equals(null) or R.isNil (if you don't care about undefined values as well):

const { reject, equals } = R

const fn = reject(equals(null)) // use R.isNil instead of R.equals if you don't care about undefined values as well

const obj = {"a":null,"b":null,"c":null,"d":{"isOk":"Yes"},"e":{"isOk":"No"},"f":{"label":"field"}}

const result = fn(obj)

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.0/ramda.js"></script>

With vanilla JS you can use the same idea with Object.entries() to convert to [key, value] pairs, filter the pairs and remove pairs with a null value, and then convert back to an object using Object.fromEntries():

const fn = onj => Object.fromEntries(
  Object.entries(obj)
    .filter(([, v]) => v !== null)
)

const obj = {"a":null,"b":null,"c":null,"d":{"isOk":"Yes"},"e":{"isOk":"No"},"f":{"label":"field"}}

const result = fn(obj)

console.log(result)
Ori Drori
  • 183,571
  • 29
  • 224
  • 209
2

This is all dynamic, what you are looking for. Hope this helps!

let A = {
            'a': null,
            'b': null,
            'c': null,
            'd': { isOk : 'Yes' },
            'e': { isOk : 'No' },
            'f': { label : 'field'}
       };

var res = {};
Object.keys(A).forEach(key => {
    if(A[key]!==null){
        res[key]=A[key];
    }
})

console.log(res);
Harmandeep Singh Kalsi
  • 3,315
  • 2
  • 14
  • 26
1

Ramda has a function filter for filtering an object or array by predicate, and its counterpart for keeping only those elements that don't match the predicate, reject.

It also has a predicate isNil to check whether a value is null or undefined.

We can put them together to write reject (isNil) to get a function which does what you're requesting:

let A = {a: null, b: null, c: null, d: {isOk: 'Yes'}, e: {isOk: 'No'}, f: { label : 'field'}}

console .log (reject (isNil) (A))
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.27.0/ramda.js"></script>
<script> const {reject, isNil } = R                                  </script>
Scott Sauyet
  • 49,207
  • 4
  • 49
  • 103
1

You can use Object.keys() method to get the array of keys from the object and then iterate over the object.

let A = {
    'a': null,
    'b': null,
    'c': null,
    'd': { isOk: 'Yes' },
    'e': { isOk: 'No' },
    'f': { label: 'field' }
}

let keysArr = Object.keys(A);

keysArr.map(key => {
    if (A[key] == null)
        delete A[key]
})

console.log(A)
Madhur Bansal
  • 726
  • 2
  • 8
  • 14