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

102
Visualizações
unable to change the items based on id and expected result should be in output format

unable to change the items based on id and expected result should be in output format

const items = [
        { id: 1, value: "first" },
        { id: 2, value: "second" },
        { id: 3, value: "third" }
      ];
      const expectedOutput = [
{ id: 1, value: "first" },
        { id: 2, value: "newvalue" },
        { id: 3, value: "third" }
]
      function getData(value, id) {
       return items.map((_each)=> {
         if(_each.id === id) {
           //need to update items with id=2
         }
       })
      }
      
      console.log(getData("newvalue", 2))

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

0

let inputArr = {
  "data": [{
      "id": 1,
      "value": "first",
      "row": "A"
    },
    {
      "id": 2,
      "value": "second",
      "row": "A"
    },
    {
      "id": 3,
      "value": "Third",
      "row": "B"
    },
    {
      "id": 4,
      "value": "Fourth",
      "row": "B"
    }
  ]
}

function format(inputArr) {
  let arr = []
  let obj = {};
  inputArr.data.forEach(el => {
    obj = {
      ...obj,
      [el.row]: [...(obj[el.row] || []) , el.value],
    }

  });
  arr.push(obj);
  return arr;
}
let outputArr = format(inputArr)
console.log(outputArr)

let expectedOutput = [{
  "A": ["first", "second"]
}, {
  "B": ["Third", "Fourth"]
}]

about 4 years ago · Juan Pablo Isaza Relatório

0

@chidananda, Map callback should return updated item. Minor modification to your code would work!

const items = [
        { id: 1, value: "first" },
        { id: 2, value: "second" },
        { id: 3, value: "third" }
      ];
      const expectedOutput = [
{ id: 1, value: "first" },
        { id: 2, value: "newvalue" },
        { id: 3, value: "third" }
]
      function getData(value, id) {
       return items.map((_each)=> {
         if(_each.id === id) {
           _each.value = value;
         }
         return _each; // Return the modified item
       })
      }
      
      console.log(getData("newvalue", 2))

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