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

108
Visualizações
Push in array only values ​that contain a certain string

I would like only data containing a certain string to be sent from my array to another array for example

test=something
test1=something
test=testsadsad

If my array contains test= My new array will be ['something', 'testsadsad'] This is my code.

let data = Object.values(args);
        let serializedData = data.join("\n");
        let newArray = [];

        for (let i = 0; i < data.length; i++) {
            if (data[i].includes("test=") === true) {
                console.log(data[i])
                newArray.push(data[i].split("test="));
            }
        }
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You can modify your logic like this

//simulated data
const data = [
  "test=something",
  "test1=something",
  "test=testsadsad",
]

let newArray = [];

for (let i = 0; i < data.length; i++) {
  //`key` is `test` and `value` is `something`
  const [key, value] = data[i].split("=") //splitted by `=`
  if (key === "test") {
    newArray.push(value);
  }
}

console.log(newArray)

about 4 years ago · Juan Pablo Isaza Relatório

0

You are not formatting the data correctly when pushing onto the array.

let data = Object.values(args);
let newArray = [];

for (let i = 0; i < data.length; i++) {
    if (data[i].includes("test=") === true) {
        console.log(data[i])
        // Note It looks like your problem is here
        newArray.push(data[i].replace("test=", ''));
    }
}
about 4 years ago · Juan Pablo Isaza Relatório

0

If your input array is data then this will return the desired output:

let output = data
    .filter(item => item.includes("test="))
    .map(item => item.split("test=")[1])

This filters the array by selecting all items that include test= and then maps each result to the substring after test=.

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