'Javascript - Remove array elements if all elements are null

Cosider the following array:

let array = [
 {Product Title: "Milk", Product Variant: "2L", Quantity: "3"},
 {Product Title: "Water", Product Variant: "", Quantity: "3"},
 {Product Title: "Pepsi", Product Variant: "", Quantity: ""},
 {Product Title: "", Product Variant: "", Quantity: ""}
 {Product Title: "", Product Variant: "", Quantity: ""}
]

How do I remove elements from the array, if all the elements have no value?

What I've tried:

let contents = []

for (let i in array) {
  Object.keys(array[i]).forEach((k) => array[i][k] == "" && delete array[i][k])
  contents.push(array[i])
}

console.log(contents)

but this returns:

0: {Product Title: "Milk", Product Variant: "2L", Quantity: "3"},
1: {Product Title: "Water", Quantity: "3"},
2: {Product Title: "Pepsi"},
3: {}
4: {}

While I would want:

0: {Product Title: "Milk", Product Variant: "2L", Quantity: "3"},
1: {Product Title: "Water", Product Variant: "", Quantity: "3"},
2: {Product Title: "Pepsi", Product Variant: "", Quantity: ""}


Solution 1:[1]

This should do it:

const array = [
  {"Product Title": "Milk", "Product Variant": "2L", Quantity: "3"},
  {"Product Title": "Water", "Product Variant": "", Quantity: "3"},
  {"Product Title": "Pepsi", "Product Variant": "", Quantity: ""},
  {"Product Title": "", "Product Variant": "", Quantity: ""},
  {"Product Title": "", "Product Variant": "", Quantity: ""}
 ]
const res=array.filter(e=>Object.values(e).join("")>"")

console.log(res)

Solution 2:[2]

try this:

let newArray = contents.filter(value => Object.keys(value).length !== 0);

Use filter on your array to have a new one without empty objects

Solution 3:[3]

Objective is to filter the array and retain only those objects where at least one value is not-empty (ie, element's length is above 0).

Sample-code

const getFilteredArray = (arr = array) => (
  arr.filter(obj => Object.values(obj).reduce(
    (f, i) => (f || i.length > 0),
    false
  ))
);

Explanation

  • Use filter to keep only specific objects in the array
  • Use .reduce to iterate over each object's values
  • The aggregator f is initially assumed to be false
  • If any one value is non-empty (ie, length > 0), then aggregator is set to true

Code Snippet

let array = [
 {'Product Title': "Milk", 'Product Variant': "2L", Quantity: "3"},
 {'Product Title': "Water", 'Product Variant': "", Quantity: "3"},
 {'Product Title': "Pepsi", 'Product Variant': "", Quantity: ""},
 {'Product Title': "", 'Product Variant': "", Quantity: ""},
 {'Product Title': "", 'Product Variant': "", Quantity: ""}
];

const getFilteredArray = (arr = array) => (
    arr.filter(obj => Object.values(obj).reduce(
    (f, i) => (f || i.length > 0),
    false
  ))
);

console.log(getFilteredArray());

Improvement: Consider using Array .some instead of .reduce.

This uses .some instead of .reduce.

const getFilteredArray = (arr = array) => (
  arr.filter(obj => Object.values(obj).some(
    v => v.length > 0)
  )
);

Solution 4:[4]

Use array.splice(...) to delete or replace at a specific array index or range.

Or array.filter(...) to create a new array with only elements fitting a certain criteria.

UPDATE: delete(array[index] ) will not reindex the array after deleting the item, nor the change the array length or the indices of adjacent members.

Solution 5:[5]

You could use filter, Object.values, and join. Here is the example:

let array = [
 {ProductTitle: "Milk", ProductVariant: "2L", Quantity: "3"},
 {ProductTitle: "Water", ProductVariant: "", Quantity: "3"},
 {ProductTitle: "Pepsi", ProductVariant: "", Quantity: ""},
 {ProductTitle: "", ProductVariant: "", Quantity: ""},
 {ProductTitle: "", ProductVariant: "", Quantity: ""}
]

result = array.filter(e => Object.values(e).join(''));

console.log(result);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Carsten Massmann
Solution 2 Mehrad Alinezhad
Solution 3
Solution 4
Solution 5 M Adyan Rohutomo