0

Context: On my e-shop website, on a product listing page, a customer can select one or several keywords (tags) to filter products.

All products are in a array (products) and the selected keywords are in tags.

// tags filter
var listProduct = []
for(var i = 0; i < tags.length; i++){
  listProduct[i] = products.filter(e => {
     if(e.tags){
        return (e.tags.includes(tags[i].toLowerCase())) ? true : false
     }else{
        return false
     }
  });
}


I now have several arrays (listProduct), I know I have tags.length of them. I have to use the concat() function to concat all theses arrays, but i don't know how to do...

products = listProduct[0].concat(listProduct[1], listProduct[2] .... listProduct[?])

Thanks a lot! Vincent

2
  • 1
    FYI: includes already returns a boolean so ? true : false is redundant. Commented Aug 17, 2022 at 7:55
  • 1
    You can use flat to flatten the listProduct array. Commented Aug 17, 2022 at 7:58

2 Answers 2

2

hope this works for you. there are 2 ways:

first, you can use reduce method to concat the array:

const products = listProduct.reduce((p, c) => p.concat(c));

second, you can use flat method to flatten the original array to make it one leveled array:

const products = listProduct.flat();
Sign up to request clarification or add additional context in comments.

Comments

0

I assume that below are the data you have:

const  tags = [
    'cloth','electronic','document','entertainment'
  ];

const products  =  [
    {
    productName: "TV",
    tags: ['electronic','entertainment'
      ]
    },
    {
      productName: "Phone",
      tags: [
        'electronic'
      ]
    },
    {
      productName: "Book",
      tags: [
        'document'
      ]
    },
    {
      productName: "Shirt",
      tags: [
        'cloth'
      ]
    },
    {
      productName: "Washing Machine",
      tags: [
        'electronic'
      ]
    },
    {
      productName: "EBook",
      tags: [
        'NA'
      ]
    }
  ]
}

Now you want to filter it by tags and concat the results into an array?

const  tags = [
    'cloth','document','entertainment'
  ];

const products  =  [
    {
    productName: "TV",
    tags: ['electronic','entertainment'
      ]
    },
    {
      productName: "Phone",
      tags: [
        'electronic'
      ]
    },
    {
      productName: "Book",
      tags: [
        'document'
      ]
    },
    {
      productName: "Shirt",
      tags: [
        'cloth'
      ]
    },
    {
      productName: "Washing Machine",
      tags: [
        'electronic'
      ]
    },
    {
      productName: "EBook",
      tags: [
        'NA'
      ]
    }
  ]
  
  const finalArray = products.filter(product => { 
    return tags.find(tag =>  product.tags.includes(tag))
  })
  
  
  console.log("finalArray", finalArray)
  
  
  

So in this way you will get the result using ES6

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.