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

164
Views
Find Unique value from an array based on the array's string value (Javascript)

so I want to find unique values from an array. so for example I have this array:

const mainArr = ['shape-10983', 'size-2364', 'size-7800', 'size-4602', 'shape-11073', 'size-15027', 'size-15030', 'size-15033', 'height-3399', 'height-5884']

so I want to find the first matching value for each unique item. for example, in the array, I have two strings with the shape prefix, six items with the size prefix, and two items with the height prefix. so I want to output to be something like

const requiredVal = ["shape-10983", "size-2364", "height-3399"]

I want only the first value from any set of different values.

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

0

the simplest solution will be to iterate on the list and storing what you got in a dictionary

function removeSimilars(input) {
    let values = {};
    for (let value of input) {//iterate on the array
        let key = value.splitOnLast('-')[0];//get the prefix
        if (!(key in values))//if we haven't encounter the prefix yet
            values[key] = value;//store that the first encounter with the prefix is with 'value'
    }
    return Object.values(values);//return all the values of the map 'values'
}

a shorter version will be this:

function removeSimilars(input) {
    let values = {};
    for (let value of input)
        values[value.splitOnLast('-')[0]] ??= value;
    return Object.values(values);
}
about 4 years ago · Juan Pablo Isaza Report

0

If, as you mentioned in the comments, you have the list of prefixes already available, then all you have to do is iterate over those, to find each first element that starts with that prefix in your full list of possible values:

const prefixes = ['shape', 'size', 'height'];
const list = ['shape-10983', 'size-2364', 'size-7800', 'size-4602', 'shape-11073', 'size-15027', 'size-15030', 'size-15033', 'height-3399', 'height-5884']

function reduceTheOptions(list = [], prefixes = [], uniques = []) {
  prefixes.forEach(prefix => 
    uniques.push(
      list.find(e => e.startsWith(prefix))
    )
  );
  return uniques;
}

console.log(reduceTheOptions(list, prefixes));

about 4 years ago · Juan Pablo Isaza Report

0

You could split the string and get the type and use it aks key for an object along with the original string as value. At result take only the values from the object.

const
    data = ['shape-10983', 'size-2364', 'size-7800', 'size-4602', 'shape-11073', 'size-15027', 'size-15030', 'size-15033', 'height-3399', 'height-5884'],
    result = Object.values(data.reduce((r, s) => {
        const [type] = s.split('-', 1);
        r[type] ??= s;
        return r;
    }, {}));

console.log(result);

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!