在没有任何空值的对象数组中过滤掉对象

时间:2019-08-04 19:32:32

标签: javascript arrays object

我正在尝试过滤不包含任何空值的api输出_ api输出(JSON)如下所示:

[{ 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/' } ]

我目前拥有的代码可以实现此目的,但是它是如此庞大,并使用了很多的条件语句-我想避免这种情况。这样的事情可以实现吗?

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])
    }
}

输出如下:

   [{ 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 个答案:

答案 0 :(得分:2)

您可以通过检查所有值来过滤数组。

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; }

答案 1 :(得分:0)

您可以使用过滤器功能。 项目变量包含您的原始数据。

SELECT ... INTO DUMPFILE
相关问题