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

124
Visualizações
concatenate after in JAVASCRIPT

I have an array of strings.

For e.g. column_list=["ab","bc","cd","asv_a","asv_f","asv_l","asv_x", "xy","yz"].

Now I have another array with only the"asv_" value(which can be repeated or unique).

For e.g. new_column = ["asv_b", "asv_f", "asv_s"].

Now I want to create a new array by concatenating the new_column array to column_list such that all the asv_ values be together in the middle and unique.

Expected outcome -

result = ["ab","bc","cd","asv_a","asv_f","asv_l","asv_x","asv_b", "asv_s", "xy","yz"]
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can use method mergedArr to create an array with Array.prototype.reduce() to keep the order and then with Object.values() and Array.prototype.flat() to build the result as a Set of unique values

Code:

const column_list = ["ab","bc","cd","asv_a","asv_f","asv_l","asv_x", "xy","yz"]
const new_column = ["asv_b", "asv_f", "asv_s"]
const mergedArr = (arr, newArr) => Object
  .values(
    arr.reduce(
      (a, c, i) => (a[c.startsWith('asv_') ? 1 : !a[1].length ? 0 : 3].push(c), a), 
      [[], [], newArr, []]
    )
  )
  .flat()
  
const result = [...new Set(mergedArr(column_list, new_column))]

console.log(result)

about 4 years ago · Juan Pablo Isaza Relatório

0

First, create a Set of asv_* values in column_list for use in eliminating duplicates

const asvValues = new Set(column_list.filter(v => v.startsWith("asv_")));

Then filter out duplicates

const inserts = new_column.filter(v => !asvValues.has(v));

Now, find the index of the last asv_ entry

const lastAsv = column_list.lastIndexOf([...asvValues].pop());

Now use splice() to insert the filtered array after the position found

column_list.splice(lastAsv + 1, 0, ...inserts);

const column_list=["ab","bc","cd","asv_a","asv_f","asv_l","asv_x", "xy","yz"];
const new_column = ["asv_b", "asv_f", "asv_s"];

const asvValues = new Set(column_list.filter((v) => v.startsWith("asv_")));
const inserts = new_column.filter((v) => !asvValues.has(v));
const lastAsv = column_list.lastIndexOf([...asvValues].pop());
column_list.splice(lastAsv + 1, 0, ...inserts);

console.log(column_list);

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