I need to remove the empty string from the array inside another array and I need to return the whole array without an empty string, can anyone help me with this, array is below
const testArr = [
{
code: 'size',
name: 'Size',
options: ['small', "", "", "large"],
},
{
code: 'color',
name: 'COlor',
options: ['black', "", "", "red"],
},
]
I need result like this (without empty string)
[
{
code: 'size',
name: 'Size',
options: ['small', "large"],
},
{
code: 'color',
name: 'COlor',
options: ['black', "red"],
},
]
Use .map()
method to iterate over testArr
and transform its elements and use .filter()
method on the options
array in each object, to filter out the empty strings.
const testArr = [ { code: 'size', name: 'Size', options: ['small', "", "", "large"] }, { code: 'color', name: 'COlor', options: ['black', "", "", "red"] } ]; const result = testArr.map(obj => { obj.options = obj.options.filter(s => s.trim().length > 0); return obj; }); console.log(result);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.