0

I'm trying to use:

1..cards**.filter** method to find cards number === TRUE, 2..then use cards**.map** method to innerHTML cards.symbol value without the number value.

The end result should be 9 divs displayd in my index.html file -> <div class="cards">1</div> <div class="cards">2</div> <div class="cards">3</div> <div class="cards">4</div> <div class="cards">5</div> <div class="cards">6</div> <div class="cards">7</div> <div class="cards">8</div> <div class="cards">9</div>

//HTML

 <body>
    <h1>Filter Method</h1>
    <div class="container" id="filter-method"></div>
</body>

//JAVASCRIPT

const cards = [
    {  symbol: '1', number: true},
    {  symbol: '2', number: true},
    {  symbol: '3', number: true},
    {  symbol: '4', number: true},
    {  symbol: '5', number: true},
    {  symbol: '6', number: true},
    {  symbol: '7', number: true},
    {  symbol: '8', number: true},
    {  symbol: '9', number: true},
    {  symbol: '+', number: false},   
    {  symbol: '-', number: false},   
    {  symbol: '*', number: false} 
    ];
    
    const cardsContainer = document.querySelector('#filter-method');
    
    const filteredCards = cards.filter((card) => {
      if(card.number === true){
        return '<div class="card">' + '<span>' + card.symbol  + '<span>' + '</div>';
      }
})

cardsContainer.innerHTML = filteredCards.join('\n');
1
  • 1
    try this cards.filter(v=>v.number).map(v=><div class="card"><span>${v.symbol}</span></div>).join('\n');; Commented Apr 28, 2021 at 9:57

2 Answers 2

1

filter should return true or false to determine whether to include that item and then map can be used to turn it to html. I've assumed you want to keep the <span> elements too.

const cards = [
    {  symbol: '1', number: true},
    {  symbol: '2', number: true},
    {  symbol: '3', number: true},
    {  symbol: '4', number: true},
    {  symbol: '5', number: true},
    {  symbol: '6', number: true},
    {  symbol: '7', number: true},
    {  symbol: '8', number: true},
    {  symbol: '9', number: true},
    {  symbol: '+', number: false},   
    {  symbol: '-', number: false},   
    {  symbol: '*', number: false} 
    ];
    
const cardsContainer = document.querySelector('#filter-method');
    
const filteredCards = cards.filter(card => card.number).map(card => {
    return '<div class="card">' + '<span>' + card.symbol  + '</span>' + '</div>';
})
    
cardsContainer.innerHTML = filteredCards.join("\n")
<div class="container" id="filter-method"></div>

Sign up to request clarification or add additional context in comments.

3 Comments

Lovely, Thank you for your time Jamiec. That's exaclty what I was trying to achieve.
Jamiec, I'm having hard time to understant this function with multiple methos. const filteredCards = cards.filter(card => card.number).map(card => { Can you please try to explain it to me in the simpliest way possible?
@PaulC both filter and map are methods which act on an array, and return an array. So they are just chained one after another. You could do it in multiple steps too - const filteredCards = cards.filter(....) and then const html = filteredCards.map(...). Does that make more sense?
1

You could do everything in one go using Array.prototype.reduce():

const cards = [
  { symbol: '1', number: true },
  { symbol: '2', number: true },
  { symbol: '3', number: true },
  { symbol: '4', number: true },
  { symbol: '5', number: true },
  { symbol: '6', number: true },
  { symbol: '7', number: true },
  { symbol: '8', number: true },
  { symbol: '9', number: true },
  { symbol: '+', number: false },
  { symbol: '-', number: false },
  { symbol: '*', number: false }
];

const cardsContainer = document.querySelector('#filter-method');

cardsContainer.innerHTML = cards.reduce((strHTML, card) => {
  if (card.number) strHTML += `<div class="card"><span>${card.symbol}</span></div>`
  return strHTML;
}, '')
<body>
  <h1>Filter Method</h1>
  <div class="container" id="filter-method"></div>
</body>

2 Comments

Secan thank you for you time and good answer, most definatly gonna use that in the future. But for now I'm still on learning stages and I was trying to practice and understand .filter and .map methods..
Just to give you an idea of how "powerful" the Array.prototype.reduce() method is, just consider this: with .filter() + .map() + .join() you have a total of 30 iterations over the cards array and its filtered subset (12 for filtering, 9 for mapping, 9 for joining); with .reduce() you have only 12 iterations to obtain the same result. That is definitely a tool you want to add to your kit once you are confident with .map() and .filter() ;)

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.