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

345
Visualizações
Foreach to Variable Javascript / React

i want to ask something about how to join data inside variable, so basicly I have data that I foreach (because i need to change that object), then I want to enter each data that I foreach into a variable, as below:

const thisData = {["abcd","efgh"]}
for (let objData of thisData) {
    //lets say i need to change every data from word to number
    //I've managed to change it
    const newData = changetoNumber(objData)
}

then i need to save it to another variable, and now i'm stuck, can you guys help me?

//this is what i need
console.log(listnewData)
//1234 5678
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You are looking for a "map".

So, the map method takes in a single parameter, a function which is called on each item, in the simplest case with a single parameter, the item itself like so:


function f(x) {
    return x.length // for example
}

const thisdata = ["abcd", "efg"];
const newvar = thisdata.map(f);
console.log(newvar); // prints [3,2]
about 4 years ago · Juan Pablo Isaza Relatório

0

You can use the map function to create output in the form of an array.

const start = 96; // char code of 'a' is 97 

const thisData = ["abcd","efgh"]

const toNumber = (str) => {
  const strArr = str.split('');
  return strArr.map(s => s.charCodeAt() - start).join('');
}

console.log(thisData.map(toNumber))

about 4 years ago · Juan Pablo Isaza Relatório

0

I think your Data is not proper. It may be like This

let thisData = {
     numbers : ["asas" , "sasas"],
}

for that Solution might be like this

for(const i in thisData){
  thisData.numbers = thisData[i].map((x, index) => {
    if(typeof(x) == "string"){
      return index + 1;
  }
})

}
console.log(thisData);

And your Final output is :

{
  numbers: [1, 2]
}

You can also Try out this in my JSfiddle Playground Link : https://jsfiddle.net/anks_patel/5tLnrm6c/

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