0

I am trying to remove empty array object from my array. I have tried filter method but wont work as my array is complex one.

const input = 
[
  { daysIn: 1, daysOut: 1, category: "Day Shift" },
  { daysIn: 1, daysOut: 1, category: "Day Shift" },
  { daysIn: null, daysOut: null, category: null }
];
3
  • What defines an "empty array object"? It's not obvious from your sample data. What is your expected output? Commented Feb 4, 2021 at 7:06
  • my bad just updated the question Commented Feb 4, 2021 at 7:09
  • What have you tried so far? What the expected result do you want? Commented Feb 4, 2021 at 7:19

4 Answers 4

3

You could use Array.filter with Array.some to include any object which has any non-null value:

const data = [{ daysIn: 1, daysOut: 1, category: "Day Shift" },{ daysIn: 1, daysOut: 1, category: "Day Shift" },{ daysIn: null, daysOut: null, category: null }];

const result = data.filter(o => Object.values(o).some(v => v !== null));

console.log(result);

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

2 Comments

To be honest, I don't know we should use every or some. LOL
@Phong Using some removes the need to negate the result, so it makes it easier to read IMO.
0

You could use filter and every

Approach here is ot reject every objects that have the all falsy values

x == null will return true for x of null/undefined

const data = [
  { daysIn: 1, daysOut: 1, category: "Day Shift" },
  { daysIn: 1, daysOut: 1, category: "Day Shift" },
  { daysIn: null, daysOut: null, category: null },
  {}
];

const res = data.filter((d) => !Object.values(d).every((v) => v == null));

console.log(res);

Comments

0

You can check length of object to determine it's empty or not.

Update solution:

const data = [
  { daysIn: 1, daysOut: 1, category: "Day Shift" },
  { daysIn: 1, daysOut: 1, category: "Day Shift" }, 
  { daysIn: null, daysOut: null, category: null }];
  
const filterEmptyObject = data => data.filter(obj => Object.values(obj).every(o => {
  if(!o) { 
    // this will also check for undefined, empty string, 0, false, NaN.
    return false;
  }
  return true;
}));

const filteredData = filterEmptyObject(data);

console.log(filteredData)

Comments

0

You should use every or some based on your requirement.

  • Use every when you want to filter value if any object having all properties is null
  • Use some when you want to filter value if any object having any property is null

In your case "remove empty array object", I decided use every.

const input = 
[
  { daysIn: 1, daysOut: 1, category: "Day Shift" },
  { daysIn: 2, daysOut: 2, category: "Day Shift" },
  { daysIn: null, daysOut: null, category: null }
];

const ouput = input.filter(r => Object.values(r).every(c => c !== null));
console.log(ouput);

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.