skip to Main Content

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


  1. You can use Array.prototype.flat() to flatten the nested array structure into a single-level array. Then, use Array.prototype.filter() to filter the images based on the conditions like the following way:

    const 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
        }
      ]
    ];
    
    //filter the images based on the conditions "show_img: true" and "publish: true"
    const res = array.flat().filter(item => item.show_img && item.publish);
    
    console.log(res);
    Login or Signup to reply.
  2. You can use filter() method.

    const 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
       }
      ]
    ]
    
     //If you want to keep the structure of your initial array as it is then you can use this.
      const filterredArray = array.filter(item => item.length && item[0].show_img && item[0].publish);
     
     //If you want to flattern your array then use `flat()`
     const filterredflatternArray = array.flat().filter(item => item.show_img && item.publish);
    
     console.log("filterredArray", filterredArray);
     console.log("filterredflatternArray", filterredflatternArray);
    Login or Signup to reply.
  3. Code:

    const data = [
      [
        {
          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
        }
      ]
    ];
    
    const filteredImages = [];
    data.forEach((sublist) => {
      sublist.forEach((image) => {
        if (image.show_img && image.publish) {
          filteredImages.push(image);
        }
      });
    });
    
    console.log(filteredImages);

    Output

    [
        {
            "id": 111,
            "img_name": "Image 1.jpg",
            "img_url": "https://www.abc.png",
            "show_img": True,
            "publish": True
        }
    ]
    

    In the code snippet, filteredImages is an empty array initially. The forEach() method is used to iterate over each sublist in the data array. Within each sublist, the forEach() method is used again to iterate over each image object. The condition if (image.show_img && image.publish) checks if the show_img property is true and the publish property is true for each image. If the condition is satisfied, the image object is pushed into the filteredImages array.

    Finally, the filteredImages array contains the filtered images that meet the specified conditions.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search