Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

169
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda