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?

1
  • 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])); Commented Mar 16, 2023 at 8:28

2 Answers 2

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)

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

Comments

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)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.