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.
CodePudding user response:
- 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);
CodePudding user response:
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)
CodePudding user response:
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 );
CodePudding user response:
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
});