0
  top_products: [
    {
      "name": "Unknown New Perfume",
      "total_quantity": 8,
      "total_sales": {
        "cents": 160000,
        "currency_iso": "MYR"
      }
    },
    {
      "name": "product2",
      "total_quantity": 5,
      "total_sales": {
        "cents": 11500,
        "currency_iso": "MYR"
      }
    },
    {
      "name": "Lolo perfume",
      "total_quantity": 4,
      "total_sales": {
        "cents": 3600,
        "currency_iso": "MYR"
      }
    },
    {
      "name": "product1",
      "total_quantity": 3,
      "total_sales": {
        "cents": 3600,
        "currency_iso": "MYR"
      }
    }
  ]

I have this array which has 4 objects. I am trying to get the element where the total_sales.cents value has the same value.
How to loop through every element and check which element in total_sales.cents has the same value?

Gerald
  • 69
  • 8
  • Please visit the [help], take the [tour] to see what and [ask]. Do some research - [search SO for answers](https://www.google.com/search?q=javascript+iterate+object+site%3Astackoverflow.com). If you get stuck, post a [mcve] of your attempt, noting input and expected output using the [\[<>\]](https://meta.stackoverflow.com/questions/358992/ive-been-told-to-create-a-runnable-example-with-stack-snippets-how-do-i-do) snippet editor. – mplungjan Mar 16 '23 at 08:19
  • 1
    The dupe I posted would look like this for you: `const lookup = top_products.reduce((a, e) => { a[e.total_sales.cents] = ++a[e.total_sales.cents] || 0; return a; }, {}); console.log(top_products.filter(e => lookup[e.total_sales.cents]));` – mplungjan Mar 16 '23 at 08:28

2 Answers2

1

const top_products = [{
    "name": "Unknown New Perfume",
    "total_quantity": 8,
    "total_sales": {
      "cents": 160000,
      "currency_iso": "MYR"
    }
  },
  {
    "name": "product2",
    "total_quantity": 5,
    "total_sales": {
      "cents": 11500,
      "currency_iso": "MYR"
    }
  },
  {
    "name": "Lolo perfume",
    "total_quantity": 4,
    "total_sales": {
      "cents": 3600,
      "currency_iso": "MYR"
    }
  },
  {
    "name": "product1",
    "total_quantity": 3,
    "total_sales": {
      "cents": 3600,
      "currency_iso": "MYR"
    }
  }
]

// groups objects by total_sales.cents
const groups = top_products.reduce((groups, obj) => {
  // if not group crete group
  if (!(obj.total_sales.cents in groups)) {
    groups[obj.total_sales.cents] = []
  }
  // push object into group
  groups[obj.total_sales.cents].push(obj)
  return groups
}, {})

// get groups that are larger than 1
const duplicates = Object.values(groups).filter(group => group.length > 1)

console.log(duplicates)
Konrad
  • 21,590
  • 4
  • 28
  • 64
1

const top_products = [
    {
      "name": "Unknown New Perfume",
      "total_quantity": 8,
      "total_sales": {
        "cents": 160000,
        "currency_iso": "MYR"
      }
    },
    {
      "name": "product2",
      "total_quantity": 5,
      "total_sales": {
        "cents": 11500,
        "currency_iso": "MYR"
      }
    },
    {
      "name": "Lolo perfume",
      "total_quantity": 4,
      "total_sales": {
        "cents": 3600,
        "currency_iso": "MYR"
      }
    },
    {
      "name": "product1",
      "total_quantity": 3,
      "total_sales": {
        "cents": 3600,
        "currency_iso": "MYR"
      }
    }
  ]

let centsArr = [];
let duplicateItems = [];


top_products.forEach(item => {
  if (!centsArr.includes(item.total_sales.cents)) {
    centsArr.push(item.total_sales.cents)
  } else {
    duplicateItems.push(item.total_sales.cents)
  }
})

let result = top_products.filter(ele => {
  if (duplicateItems.includes(ele.total_sales.cents)) {
      return ele;
  }
});

console.log(result)
Ahmad Faraz
  • 1,371
  • 1
  • 4
  • 11