0

when a value of a property of the object is null or contains "missing" the whole object should be filterd.

this works for filtering null

object = (object.filter(o => !Object.values(o).includes(null)) 

But how can I add 2 filters and how to implement a filter that filters strings that contain the word "missing"

  object = (object.filter(o => !Object.values(o).includes(null) | ("missing")));

object:

[
      { id: 'blockstack-iou',
        name: 'Blockstack (IOU)',
        image: 'missing_large.png'
      }
      { id: 'qtum',
        name: 'Qtum',
        image:
         'https://assets.coingecko.com/coins/images/684/large/qtum.png?1547034438',
        price_change_percentage: -53.2869774915231
      }
    ]
0

3 Answers 3

3

Use Array.prototype.every().

Use && to combine multiple tests.

let object = [{
    id: 'blockstack-iou',
    name: 'Blockstack (IOU)',
    image: 'missing_large.png'
  },
  {
    id: 'qtum',
    name: 'Qtum',
    image: 'https://assets.coingecko.com/coins/images/684/large/qtum.png?1547034438',
    price_change_percentage: -53.2869774915231
  }
];

console.log(object.filter(o => Object.values(o).every(prop => 
  prop != null && !prop.toString().includes("missing"))));

Sign up to request clarification or add additional context in comments.

Comments

1

Use && and match

const object = [
    {id: 'blockstack-iou',
     name: 'Blockstack (IOU)',
     image: 'missing_large.png'
    },
    { id: 'qtum',
    name: 'Qtum',
    image:
    'https://assets.coingecko.com/coins/images/684/large/qtum.png?1547034438',
    price_change_percentage: -53.2869774915231
    }
  ]
const filtered = (object.filter(o =>!Object.values(o).includes(null) && !Object.values(o).toString().match(/missing/gi)));
console.log(filtered)

Comments

1

If you're looking for a solution that is easily extensible, consider storing all your "conditions" as an array and running through them using .every().

const object = [
  {str: "this_is_missing"},
  {str: null},
  {str: "valid"}
];

const validations = [
  i => i !== null,                          //Value isn't null
  i => !i.toString().includes("missing")    //Value doesn't contain "missing"
];

const validate = (val) => validations.every(fn => fn(val));

const result = object.filter(obj => Object.values(obj).every(validate));
console.log(result);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.