-1

I am having array of objects through which I have to iterate and return the particular object whose isPremium property value is true isPremium === true, I have used .forEach and .map function to achieve the functionality, but my solution is not filtering according to condition, below is my array and my solution, Can anyone help, thanks in advance

  const brands = [
           {id:1, brand_name: 'adidas', brand_trans: 'أديداس', isPremium: true, url: '/brands/adidas'},
           {id:2, brand_name: 'adidas Originals', brand_trans: 'أديداس اوريجينال', isPremium: false, url: '/brands/adidas-originals'},
           {id:4, brand_name: 'Bodyism', brand_trans: 'بودييزم', isPremium: false, url: '/brands/bodyism'},
           {id:5, brand_name: 'Columbia', brand_trans: 'كولومبيا', isPremium: false, url: '/brands/columbia'},
           {id:5, brand_name: 'Converse', brand_trans: 'كونفرس', isPremium: true, url: '/brands/converse'},
         ];

Solution

         var premiumHeader = brands.map(function(item, index) {
            for( var key in item ) {
             return item.isPremium !== false;
           }
         });

         premiumHeader;

wali
  • 615
  • 10
  • 24
  • 4
    You're looking for `.filter()` or `.find()`. – SLaks Jun 14 '17 at 20:54
  • 1
    If you are wanting to filter elements, then use [.filter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter?v=example) – Patrick Evans Jun 14 '17 at 20:54
  • 1
    Please [search](https://stackoverflow.com/search?q=%5Bjs%5D+filter+array+of+objects) before posting. More on searching [here](/help/searching). – T.J. Crowder Jun 14 '17 at 20:56
  • 2
    I have to iterate over an array of objects and return the objects whose isPremium value is true SLaks – wali Jun 14 '17 at 20:58
  • 3
    change .map to .filter - https://codepen.io/nagasai/pen/pwEQoj for reference – Naga Sai A Jun 14 '17 at 20:58

1 Answers1

-1

Try the Array.filter function:

var premiumItems = brands.filter( item => item.isPremium );
Julian Goacher
  • 567
  • 2
  • 6