Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

107
Views
Loop into array object then check if there's a equal value?

Question, I have this array object, I want to find out which of this array have a similar values then make them as one.

Example

[0:
        cartProduct: {
    category: "chair"
    color: "navy"
    id: "628a1738fd8299ae6659d994"
    image: "http://localhost:5000/../public/Product_chair_communal-navy.jpg"
    name: "The Communal"
    price: "4.30"
    }
        quantity: 1,
        1:
        cartProduct: {{
    category: "chair"
    color: "navy"
    id: "628a1738fd8299ae6659d994"
    image: "http://localhost:5000/../public/Product_chair_communal-navy.jpg"
    name: "The Communal"
    price: "4.30"
    }
        quantity: 1,

] For example the data above I want to know if they have the similar values interms of color if yes then only return one value.

Thanks!

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can use this loop:

let uniqueArray = [];

dataArray.forEach((item, indx) => {
  let colorsArray = [];
  if (colorsArray.includes(item.color)) {
    continue;
  }
  uniqueArray.push(item);
})

about 4 years ago · Juan Pablo Isaza Report

0

Not the cleanest, or most performant approach:

// function to group the items
const groupCartItems = (items, byProperties = ['color', 'id']) => {

    // utility funciton
    const verifyEquality = (itemA, itemB) => {
        let isEqual = true;
        byProperties.forEach((prop) => {
            if (itemA.cartProduct[prop] != itemB.cartProduct[prop]) {
                isEqual = false;
                break;
            }
        });
        return isEqual;
    };

    const groupedItems = [];

    items.forEach((item) => {
        // if item has been added, skip
        if (groupedItems.find((i) => verifyEquality(item, i))) {
            return;
        }
        // find equal items
        const equals = items.filter((i) => verifyEquality(item, i));
        // sum quantities
        const quantity = equals.reduce((previousValue, data) => previousValue + data.quantity, 0);
        // push
        groupedItems.push({
            cartProduct: item.cartProduct,
            quantity,
        });
    });

    return groupedItems;
};

For the 'similarity' stuff, I would recommend not to do this, because it is just not a good practise. Have your values equal or else!!!

Now seriously, check string-similarity. From documentation, you would only need to change the if inside verifyEquality function to:

import stringSimilarity from 'string-similarity';

// tweak this value to change how similar strings should be to be considered equal
const EQUALITY_RATIO = 0.75;

// ....

if (stringSimilarity.compareTwoStrings(itemA.cartProduct[prop], itemB.cartProduct[prop]) < EQUALITY_RATIO) {
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!