I have an array of object arrays, and one of the dates is empty, I would like to remove that empty date.
const arr = [
{
"2022年03月10日": [
{ "Age": "31", "Last": "Craig", "Name": "Carlos", "Time": "11:00am - 12:00pm", "Type": "Consulta General", "read": false }
],
"2022年03月26日": [
{ "Age": "31", "Last": "Craig", "Name": "Carlos", "Time": "11:00am - 12:00pm", "Type": "Follow Up", "read": false }
],
"2022年03月27日": [],
"2022年03月31日": [
{ "Age": "31", "Last": "Craig", "Name": "Carlos", "Time": "11:00am - 12:00pm", "Type": "Valoraciones Funcionales", "read": false }
]
}
];
What I've tried
list.push(doc.data());
var filtered = list.filter(function (el) {
return el != " ";
});
I would like to keep the same array, just without the "2022年03月27日" item.
Also, if it is also possible to filter directly from the doc.data() without having to do the list.push, that would be much better.
-
1It looks like you actually want to filter the properties of the object, not filter the array.Barmar– Barmar2022年03月29日 20:48:49 +00:00Commented Mar 29, 2022 at 20:48
-
@Barmar if possible yesCarlos Craig– Carlos Craig2022年03月29日 21:51:40 +00:00Commented Mar 29, 2022 at 21:51
4 Answers 4
- Using
Array#map, iterate over the array - Using
Object#entries, get the list of entries for the current object - Using
Array#filter, filter the entries whose value is an empty array - Using
Object#fromEntries, return the current object containing the filtered entries
const arr = [
{
"2022-03-10": [
{ "Age": "31", "Last": "Craig", "Name": "Carlos", "Time": "11:00am - 12:00pm", "Type": "Consulta General", "read": false }
],
"2022-03-26": [
{ "Age": "31", "Last": "Craig", "Name": "Carlos", "Time": "11:00am - 12:00pm", "Type": "Follow Up", "read": false }
],
"2022-03-27": [],
"2022-03-31": [
{ "Age": "31", "Last": "Craig", "Name": "Carlos", "Time": "11:00am - 12:00pm", "Type": "Valoraciones Funcionales", "read": false }
]
}
];
const filtered = arr.map(obj =>
Object.fromEntries(
Object.entries(obj).filter(([key, value]) => value.length > 0)
)
);
console.log(filtered);
6 Comments
doc.data() before doing list.push(), so we don't need to map over the entire array every time we add an object to it.It looks like you need to review data structure. I restructured your code to conventional format I hope that what you mean to do. if not leave me a comment and I will recode it for you
const result = [
{
"2022-03-10": [],
"Age": "31",
"Last": "Craig",
"Name": "Carlos",
"Time": "11:00am - 12:00pm",
"Type": "Consulta General",
"read": false,
},
{
"2022-03-26": [],
"Age": "31",
"Last": "Craig",
"Name": "Carlos",
"Time": "11:00am - 12:00pm",
"Type": "Follow Up",
"read": false,
},
{ "2022-03-27": [] },
{
"2022-03-31": [],
"Age": "31",
"Last": "Craig",
"Name": "Carlos",
"Time": "11:00am - 12:00pm",
"Type": "Valoraciones Funcionales",
"read": false,
},
];
const removeEmpty = result.filter(oneObj => Object.values(oneObj).length > 1);
console.log(removeEmpty)
Comments
You can use delete to remove the parts of the object that have an empty array. This changes the original array.
const arr = [{
"2022-03-10": [
{ "Age": "31", "Last": "Craig", "Name": "Carlos", "Time": "11:00am - 12:00pm", "Type": "Consulta General", "read": false }
],
"2022-03-26": [
{ "Age": "31", "Last": "Craig", "Name": "Carlos", "Time": "11:00am - 12:00pm", "Type": "Follow Up", "read": false }
],
"2022-03-27": [],
"2022-03-31": [
{ "Age": "31", "Last": "Craig", "Name": "Carlos", "Time": "11:00am - 12:00pm", "Type": "Valoraciones Funcionales", "read": false }
]
}];
arr.forEach(e =>
Object.entries(e).forEach(([key,value]) => value.length || delete e[key])
);
console.log( arr );
Comments
the main array contains one object you can try the following solution
var filtered = Object.keys(list[0]).filter(function (el) {
return list[0][el].length >0
});