0

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: ""}
2
  • const filteredArray = array.filter(o => Object.values(o).reduce((f, i) => (f || i.length > 0) , false)); Commented Jan 30, 2022 at 9:55
  • Have you considered using Lists ? Commented Jan 30, 2022 at 10:03

6 Answers 6

4

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

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

3 Comments

You beat me to it again 😁! Always nice to see your answers! 👍
Thank you so much, it works. Really like the one line solution
@CarstenMassmann Agreed. Her answers are amazing! She is a developer from GOD.
2

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)

Comments

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

Comments

0

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

Comments

0

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.

Comments

0

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

1 Comment

Your answer is a duplicate of the accepted answer. Please make sure that any new answers are unique

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.