0

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.

2
  • 1
    It looks like you actually want to filter the properties of the object, not filter the array. Commented Mar 29, 2022 at 20:48
  • @Barmar if possible yes Commented Mar 29, 2022 at 21:51

4 Answers 4

4

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

Sign up to request clarification or add additional context in comments.

6 Comments

I was preparing a similar answer, but in mine I filtered 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.
@Barmar if its not necessary to do the push, how could i do with the doc.data()?
@Majed I ended up using this solution and modifying the code to use with the doc.data(). Thanks for saving me so much time
@CarlosCraig I never said not to do the push, I said to remove the property from the object before pushing it, instead of looping over the array and removing the property then.
@Barmar it actually ended up working better without the push, so you still assisted in allowing me to think that way
|
1

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

1

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

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.