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

262
Visualizações
replace json key with another json value using node js / javascript
testDetails.map(function(data){
 
let JsonData={
 title: data["title"],
 url: data["url"],
 firstName: data["username"],

 work:[{workid:data["workid"].slice(","), workname: "tester"}],
 employee: [{employee_id: data["employee_id"].slice(","), employee_desk:"custom"}]
}
})

so my above json output look like this

{
 "title":"01"
 "url":"employe@url.com",
 "firstName":"employee name",
 "work":[{"workid":"9371", "workname": "tester"}],
 "employee":[{"employee_id":"weh34",employee_desk:"custom"}]
}

and another JSON object where data look like this

let data={
  "031w":"ewid3728e",
  "9371":"emp_01",
   "weh34":"work_place01"
}

The workid and employee id with the JsonData I want to replace with the value which is present inside the data json so my data which is look like this

 "work":[{"workid":"9371", "workname": "tester"}],
 "employee":[{"employee_id":"weh34",employee_desk:"custom"}]

needed to be look like this

 "work":[{"workid":"work_place01", "workname": "tester"}],
 "employee":[{"employee_id":"emp_01",employee_desk:"custom"}]

I am trying to replace the code manually like this

 work:[{workid:data.weh34, workname: "tester"}],
 employee: [{employee_id: data.9371, employee_desk:"custom"}]

but data["workid"].slice(",") and data["employee_id"].slice(",") always get changed so I want to match both the json and fetch the value and place it in my JsonData

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

0

Just use the value you got from testDetails as the index in the data object to get the corresponding value.

You need to use a different variable from data as the parameter to the mapping function so you can refer to the external variable data. I changed it to d below.

testDetails.map(d => ({
  title: d["title"],
  url: d["url"],
  firstName: d["username"],
  work: [{
    workid: data[d["workid"].slice(",")],
    workname: "tester"
  }],
  employee: [{
    employee_id: data[d["employee_id"].slice(",")],
    employee_desk: "custom"
  }]
}));
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