2

I have an array and an object. In the array I have a list of Products and in the object I have a category.

What I should to do is to find a code in the array equals to the category.Code .

So I have thought to do a .map of the array, but I don't know how to return back only the values equal to category.code.

console.log("this.props.products: ", this.props.products)
console.log("this.props.category: ", this.props.category)

let findProducts = this.props.products.Products.map((product) => {
    console.log("product_", product.Categories.MainCode)
    product.Categories.MainCode == this.props.category.Code
})

console.log("findProducts: ", findProducts)

The findProducts returns me a list of undefined.

Basically I want obtain only the this.props.products.Products.Categories.MainCode == this.props.category.Code

(that should be more than one).

How can I do?? Thank you

1
  • 2
    Give us an example array of products so we can test out Commented May 18, 2020 at 13:10

6 Answers 6

1

map is not the right function to use in this case. Instead, use .filter() function which will return an array of only those products for which the callback function returns true

let findProducts = this.props.products.Products.filter((product) => {
  return product.Categories.MainCode == this.props.category.Code;
})
Sign up to request clarification or add additional context in comments.

Comments

1

What you need in this case is array.filter method, which expects a function to decide if an element should be in the result array.

let findProducts = this.props.products.Products.filter((product) => {
    return product.Categories.MainCode === this.props.category.Code
})
``

Comments

0

Try with filter

var arrayFiltered = this.props.products.Products.filter(item => item.Categories.MainCode == this.props.category.code);

Comments

0

You also need to return something from that mapping lambda...

let findProducts = this.props.products.Products.map(product => {
  console.log('product_', product.Categories.MainCode);
  return product.categories.MainCode == this.props.category.Code;
});

Besides, it looks like a use case for .filter rather than .map.

Comments

0

This sounds like a standard use of filter:

let findProducts = this.props.products.Products.filter((product) => {
   return product.Categories.MainCode == this.props.category.Code
})

Filter returns only the entries for which true is returned.

Comments

0

Try this:

let findProducts = this.props.products.Products.map((product) => {
    if (product.Categories.MainCode === this.props.category.Code) return product
})

1 Comment

map function returns a value in each iteration. In your code, when if condition will be false, undefined will be returned.

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.