Unique value in object (ES6)

Unique value in object (ES6)
0

#1

Hi,
I have this array from an API.
here the structure

      const store = 
      [
        {category: "Sporting Goods", price: "$49.99", stocked: true, name: "Football"},
        {category: "Sporting Goods", price: "$9.99", stocked: true, name: "Baseball"},
        {category: "Sporting Goods", price: "$29.99", stocked: false, name: "Basketball"},
        {category: "Electronics", price: "$99.99", stocked: true, name: "iPod Touch"},
        {category: "Electronics", price: "$399.99", stocked: false, name: "iPhone 5"},
        {category: "Electronics", price: "$199.99", stocked: true, name: "Nexus 7"}
      ]

Here my loop, I’m using props with React.js, I can retrieve the item, but I want to have the category to be unique and stay in function programming concept with pure function?

    const productAll = this.props.productAll;

    const faire = productAll
    .map(item => 
      <p key={ item.name.toString() }>
        {item.category === 'Sporting Goods'}
      </p>
    )


#2

Wouldn’t you rather use filter than map ? Map will return the exact number of elements from your original array , you can transform the elements but it will return the same number of elements, in your case the transformation would probably return a Boolean value for each element. I think filter is a way to go.

Apologies, but I don’t have access to my pc and typing on my phone so I can’t test the code .


#3

With the spread operator I did the job:

    const unique =   productAll
      .map( element =>
      element.category
    );

    console.log(unique);
    let uniqueArray = [...new Set(unique)]
    console.log(uniqueArray );