Jacopo
Jacopo

Reputation: 163

Check if two keys have the same value inside a map function in React

I am trying to check if the data I am getting from a report object has the same values under a certain key, so that I can build a chart with different labels of only one for each value I get. So far this is the code:

return (
<ChartDiv>
  <Bar
    data={{
      labels: [
        props.salesReports.map((label, i) => {
          //Check if I receive two of the same values
          return props.salesReports[i][4];
        }),
      ],
      datasets: [
        {
.......

Does anyone know if this is possible to do directly inside .map() or if the data needs to be polished beforehand? Thanks!

Upvotes: 0

Views: 1428

Answers (1)

Ahmed Gaber
Ahmed Gaber

Reputation: 3976

this example can help you

let array = ['A', 'B', 'A', 'C', 'B'];

let unique = array.filter((x, index) => array.indexOf(x) === index);

let duplicated = array.filter((x, index) => array.indexOf(x) !== index);

console.log("unique : " + unique); //[ 'A', 'B', 'C' ]

console.log("duplicated : " + duplicated ); //[ 'A', 'B' ]

Upvotes: 2

Related Questions