从Javascript中的对象数组过滤未定义 [英] Filtering undefined from array of objects in Javascript

查看:77
本文介绍了从Javascript中的对象数组过滤未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在从本地存储创建的对象数组中整理出未定义时遇到麻烦。假设该对象数组为localStorage:

I am having trouble sorting out the undefined from the array of objects that was crated from local storage. Lets assume that this array of of objects is localStorage:

var arrObject = [{date:undefined,bus_name:Thomas#1}; .. 。] 假设这有2个未定义的日期。

var arrObject = [{date: undefined, bus_name: Thomas #1};...] Assume this has 2 dates that had undefined.

我希望能够过滤出未定义的日期和在日期之内,因此,例如,如果在对对象进行排序之前使用过滤器对对象数组进行过滤,则将不包括 {date:undefined,bus_name:Thomas#1}

I want to be able to filter out the date that has undefined and the bus_name that is within the date so for example, if I used filter for an array of objects before sorting them, then {date: undefined, bus_name: Thomas #1} will not be included in the array that will be sorted.

我将如何做到这一点?

谢谢!

更新:3/5/20

如果我有多于2列,我该怎么做?至少5,我要过滤和排序日期以及仅输出日期和bus_name

How would I accomplish this if I have more than 2 columns, lets say I have at least 5, I want to filter and sort date as well as only output date and bus_name

var arrObject = [{date:undefined,bus_name :托马斯#1,公交司机:托马斯,时间开始:上午9点,时间结束:下午5点}; ...]

输出: {date:...,bus_name:...}; {...}

推荐答案

我认为您应该提供带有条件字段的过滤器(一个是日期,另一个是bus_name)

i think you should provide filter with condation field (one is date, another is bus_name )

无论如何,我想清除以下内容:-

anyway i want clear it out that :--

 i) arrObject.filter(e => e.date)  // with this all {date: undefined } contain object will be filter and get data which have actual date value 

 ii) arrObject.filter(e => e.date === undefined) // give filter result with all date undefined  
iii) arrObject.filter(e => e.date === undefined && e.bus_name ) or arrObject.filter(e => e.date && e.bus_name)

您期望的结果之一

这篇关于从Javascript中的对象数组过滤未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆