1

I'm trying to filter out api output _that doesn't include any null values - the api output (JSON) looks like so:

[{ title:
        'Bayern Munich defenders are scared of damned talented Jadon Sancho, says Borussia Dortmund team-mate Axel Witsel - Goal',
    date: '2019-08-04T11:42:00Z',
    image: null,
    description:
        'As Leeds kick off the new season with hopes of returning to the Premier League, we remember their painful demise from 15 years ago',
    link:
        'https://www.goal.com/en/news/even-bayern-defenders-are-scared-of-damned-talented-sancho/huf85pn3ob4s1r0fcjq52nsqe' },
    { title:
        'Manchester City y Liverpool se enfrentan con la Community Shield en juego: hora y TV - infobae',
    date: '2019-08-04T11:36:59Z',
    image:
        'https://www.infobae.com/new-resizer/ZvS6Vs3f7Qa_11HIibSiMy8DOUw=/1200x0/filters:quality(100)/s3.amazonaws.com/arc-wordpress-client-uploads/infobae-wp/wp-content/uploads/2019/08/04082112/Guardiola-Klopp.jpg',
    description:
        'El campeón y el subcampeón de la Premier League se enfrentan con el primer título de la temporada en juego. En la previa, hubo un duelo verbal entre Pep Guardiola y Jürgen Klopp. A las 11, por ESPN2',
    link:
        'https://www.infobae.com/america/deportes/futbol-europeo/2019/08/04/manchester-city-vs-liverpool-community-shield-2019/' },
    { title:
        'Ireland\'s Hourihane bangs in two stunning free-kicks ahead of Villa\'s Premier League return - The42',
    date: '2019-08-04T11:33:00Z',
    image:
        'https://img2.thejournal.ie/article/4752619/river/?height=400&version=4752623',
    description: null,
    link:
        'https://www.the42.ie/conor-hourihane-free-kicks-leipzig-4752619-Aug2019/' } ]

The code I currently has achieves this, but it's so bulky and uses a lot of conditionals - which I'd like to avoid. Can something like this be achieved?

let filteredArr = [];
for (let i = 0; i < data.length; i++) {
    if (data[i].title !== null &&
        data[i].date !== null &&
        data[i].image !== null &&
        data[i].description !== null &&
        data[i].link !== null) {
        filteredArr.push(data[i])
    }
}

The output would be like so:

   [{ title:
        'Manchester City y Liverpool se enfrentan con la Community Shield en juego: hora y TV - infobae',
    date: '2019-08-04T11:36:59Z',
    image:
        'https://www.infobae.com/new-resizer/ZvS6Vs3f7Qa_11HIibSiMy8DOUw=/1200x0/filters:quality(100)/s3.amazonaws.com/arc-wordpress-client-uploads/infobae-wp/wp-content/uploads/2019/08/04082112/Guardiola-Klopp.jpg',
    description:
        'El campeón y el subcampeón de la Premier League se enfrentan con el primer título de la temporada en juego. En la previa, hubo un duelo verbal entre Pep Guardiola y Jürgen Klopp. A las 11, por ESPN2',
    link:
        'https://www.infobae.com/america/deportes/futbol-europeo/2019/08/04/manchester-city-vs-liverpool-community-shield-2019/' } ]

2 Answers 2

2

You could filter the array by checking all values.

var data = [{ title: 'Bayern Munich defenders are scared of damned talented Jadon Sancho, says Borussia Dortmund team-mate Axel Witsel - Goal', date: '2019-08-04T11:42:00Z', image: null, description: 'As Leeds kick off the new season with hopes of returning to the Premier League, we remember their painful demise from 15 years ago', link: 'https://www.goal.com/en/news/even-bayern-defenders-are-scared-of-damned-talented-sancho/huf85pn3ob4s1r0fcjq52nsqe' }, { title: 'Manchester City y Liverpool se enfrentan con la Community Shield en juego: hora y TV - infobae', date: '2019-08-04T11:36:59Z', image: 'https://www.infobae.com/new-resizer/ZvS6Vs3f7Qa_11HIibSiMy8DOUw=/1200x0/filters:quality(100)/s3.amazonaws.com/arc-wordpress-client-uploads/infobae-wp/wp-content/uploads/2019/08/04082112/Guardiola-Klopp.jpg', description: 'El campeón y el subcampeón de la Premier League se enfrentan con el primer título de la temporada en juego. En la previa, hubo un duelo verbal entre Pep Guardiola y Jürgen Klopp. A las 11, por ESPN2', link: 'https://www.infobae.com/america/deportes/futbol-europeo/2019/08/04/manchester-city-vs-liverpool-community-shield-2019/' }, { title: 'Ireland\'s Hourihane bangs in two stunning free-kicks ahead of Villa\'s Premier League return - The42', date: '2019-08-04T11:33:00Z', image: 'https://img2.thejournal.ie/article/4752619/river/?height=400&version=4752623', description: null, link: 'https://www.the42.ie/conor-hourihane-free-kicks-leipzig-4752619-Aug2019/' }],
    result = data.filter(o => Object.values(o).every(v => v !== null));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

Comments

0

You can use the filter function. The items variable contains your original data.

const filtered = items.filter(function(item) {
  for (var key in item) {
    if (null == item[key])
      return false;
  }
  return true;
});

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.