This is my Array:
[
[
{
id: 111,
img_name: 'Image 1.jpg',
img_url:'https://www.abc.png',
show_img:true,
publish:true
}
],
[],
[
{
id: 333,
img_name: 'Image 3.jpg',
img_url:'https://www.ghi.png',
show_img:false,
publish:false
}
]
]
UPDATE: Based on Mamun’s answer this is the output I get
[] [] []And this is the output I need:
[
{
id: 111,
img_name: 'Image 1.jpg',
img_url:'https://www.abc.png',
show_img:true,
publish:true
}
]
Now how do I filter the images based on the condition "show_img :true and "publish: true"
3
Answers
You can use
Array.prototype.flat()
to flatten the nested array structure into a single-level array. Then, useArray.prototype.filter()
to filter the images based on the conditions like the following way:You can use
filter()
method.Code:
Output
In the code snippet,
filteredImages
is an empty array initially. TheforEach()
method is used to iterate over eachsublist
in the data array. Within eachsublist
, theforEach()
method is used again to iterate over eachimage
object. The conditionif (image.show_img && image.publish)
checks if theshow_img
property is true and thepublish
property is true for eachimage
. If the condition is satisfied, theimage
object is pushed into thefilteredImages
array.Finally, the
filteredImages
array contains the filtered images that meet the specified conditions.