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

231
Visualizações
Javascript function to modify the array of objects reuturns the same array

I have a data inside of my array, which looks like this.

const arr = [
  {
  devSth: {
    h1: [1],
    h2: [1],
  }
 },
];

I have a function, which should modify this array. It looks like this:

const modifyArr = (data) => {
 data.forEach(el => {
  let keys = el.devSth && Object.keys(el.devSth);
  keys.forEach(key => {
    el.devSth[key].map(el => {
    return {
    name: el,
    val: 'lalala'
    }
    })
  })
})
return data;
}

Then I am using my function:

const newArr = modifyArr(arr);
console.log(newArr);

Expected output:

[{
  devSth: {
    h1: [{name: 1, val: 'lalala'}],
    h2: [{name: 1, val: 'lalala'}]
  }
}]

Actual output:

[{
  devSth: {
    h1: [1],
    h2: [1]
  }
}]

Would appreciate any help.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

you are mapping the values, but not setting them (line 5) :

const modifyArr = (data) => {
 data.forEach(el => {
  let keys = el.devSth && Object.keys(el.devSth);
  keys.forEach(key => {
    el.devSth[key] = el.devSth[key].map(el => {
    return {
    name: el,
    val: 'lalala'
    }
    })
  })
})
return data;
}

this is untested.

about 4 years ago · Juan Pablo Isaza Relatório

0

You dont assign the result of the .map operation back to the original keys h1 and h2. So it doesn't modify those.

Just change

            el.devSth[key].map(el => {
                return {
                    name: el,
                    val: 'lalala'
                }
            })

to

            el.devSth[key] = el.devSth[key].map(el => {
                return {
                    name: el,
                    val: 'lalala'
                }

const arr = [{
  devSth: {
    h1: [1],
    h2: [1],
  }
}, ];

const modifyArr = (data) => {
  data.forEach(el => {
    let keys = el.devSth && Object.keys(el.devSth);
    keys.forEach(key => {
      el.devSth[key] = el.devSth[key].map(el => {
        return {
          name: el,
          val: 'lalala'
        }
      })
    })
  })
  return data;
}

const newArr = modifyArr(arr);
console.log(newArr);

about 4 years ago · Juan Pablo Isaza Relatório

0

An alternative way of doing it:

const arr = [{
  devSth: {h1: [1], h2: [1]}
}];

let obj = arr[0].devSth, number;

for (const prop in obj){
  number = obj[prop][0];
  obj[prop] = [{name: number, val: 'lalala'}];
}

console.log([{devSth: obj}])

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