1

I'm trying to filter my array availableSizes and just display a certain size that I input into includes

I am testing with 5 and it should output just the number 5 but I am getting this is not a function

How can I filter this array so it will just output the numbers passed into the includes method?

 render() {
        const { id, img, name, price, desc, color, match, material, size, slug } = this.props
        const { selectedSize, showSizes } = this.state
        const availableSizes = [5,6,7,8,9,10,11,12,13,14,15]

    return (
      <Wrap>
        <Half>
          <ImgWrap>
          {showSizes &&
            <Sizes>

              <SizeWrap>
                <div>selecta size</div>
                {availableSizes.filter(availableSize => availableSize.includes(5)).map((availableSize, index) =>
                  <Size
                    key={index}
                    active={selectedSize === availableSize.size}
                    onClick={() => this.handleSelectSize(availableSize.size)}
                    className="snipcart-add-item"
                    data-item-id={id}
                    data-item-name={name}
                    data-item-price={price}
                    data-item-url="/"
                    data-item-size={this.state.size}
                    data-item-description={desc}
                  >
                    {availableSize}
                  </Size>
                )}
              </SizeWrap>
0

2 Answers 2

3

Taking this one (likely) step further, you probably want to match available sizes to more than one value. So if you have a user who is interested in sizes 5 and 6, you might store that in an array like: var selectedSizes = [5, 6]

Then you could match against the available sizes with: availableSizes.filter(x => selectedSizes.includes(x))

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

Comments

2

availableSize from your filter callback will be a number. Number don't have method includes. What you should do is check if equal.

availableSizes.filter(availableSize => availableSize === 5)

And for the active prop you should

active={selectedSize === availableSize}

For the onClick also change for

onClick={() => this.handleSelectSize(availableSize)}

For multiple size you can then build an array of these sizes

const mySizes = [4, 5] 
availableSizes.filter(availableSize => mySizes.includes(availableSize))

5 Comments

console.log(availableSizes.includes(5)); == true, how does this work if number doesn't have this method?
availableSizes is the array. availableSize is a single number of the array availableSizes
If you look in mdn doc you will see filter callback provide you the item at certain index developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
ok so say I had 2 numbers instead of just 5 how can i check if the array includes both of these number and output the results?
build an array with both number an now check with the includes. const mySizes = [4, 5] availableSizes.filter(availableSize => mySizes.includes(availableSize))

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.