Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

313
Views
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: ""}
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

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

about 4 years ago · Juan Pablo Isaza Report

0

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)

about 4 years ago · Juan Pablo Isaza Report

0

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

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!