Home > Software engineering >  Javascript - Remove array elements if all elements are null
Javascript - Remove array elements if all elements are null

Time:01-30

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: ""}

CodePudding user response:

You could join all values and take the string for filtering.

const
    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(o => Object.values(o).join(''));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

CodePudding user response:

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)

CodePudding user response:

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

CodePudding user response:

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.

delete(obj.key) works to delete entries from objects, not arrays.

CodePudding user response:

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)
  )
);
  •  Tags:  
  • Related