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: ""}
6 Answers 6
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; }
3 Comments
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
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
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
filterto keep only specific objects in the array - Use
.reduceto iterate over each object's values - The aggregator
fis initially assumed to befalse - 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
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
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);
const filteredArray = array.filter(o => Object.values(o).reduce((f, i) => (f || i.length > 0) , false));