javascript - 过滤出没有任何空值的对象数组中的对象
问题描述
我正在尝试过滤掉不包含任何空值的 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; }
推荐阅读
- .net-core - 如何使用 ef core 3.1 管理基于 .net 4.7、ef 6 和 .net core 3.1 的项目?
- python - Pandas pivot table with different conditions in aggfunc
- javascript - 不能在 Vuex Mutation 中使用解构赋值
- python - 迭代 JSON 以获取某些元素并在之后使用它们
- nginx - 为什么 nginx URL 最后没有斜线就不能工作?
- python - 格式化德语的刻度标签,即,点作为千位分隔符,逗号作为小数分隔符
- excel - 为什么我的 VBA ComboBox 在我输入之前是空白的
- django - Django throws OperationalError : index row requires 35424 bytes, maximum size is 8191
- python-3.x - Why "a is b" false even though both have same values?
- google-app-engine - 如果我的 Google 的 App Engine 实例尝试与 MySQL 实例建立超过 100 个并发连接,会发生什么情况?