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

标签 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/' } ]

最佳答案

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

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

关于javascript - 过滤掉对象数组中没有任何空值的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57349641/

相关文章:

java - 安卓 - ImageButton.getBackgroundResource?

javascript - 使用javascript从一个函数获取值到另一个函数

javascript - 如何在第一次按下按钮时显示消息?

Javascript ES6 共享类变量

java - 彩票号码生成器

c - 错误 C2057 在使用常量时需要常量表达式

javascript - 从字符串数组中删除不包含 "IN"的所有元素

javascript - JavaScript 中如何检查对象键是否存在?

javascript - 打开带有片段标识符的 URL 时防止默认跳转?

javascript - JavaScript 中原型(prototype)链的结尾是什么——null 或 Object.prototype?