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

77
Visualizações
How to add text in key value pair Object

I had two array data which I was able to convert into a key value pair object.

firstArray: [] = ['a','b','c','d']
secondArray: [] = [1,2,3,4]

Code to convert two array to Key Value Pair:

let dict = firstArray.map(function(obj, index) {
let mydict = {}
mydict[secondArray[index]] = obj;
return mydict;
});

console.log(dict) 
Output: 
 [{
  "1": "a"
}, {
  "2": "b"
}, {
  "3": "c"
}, {
  "4": "d"
}] 

Desired Output:

[{
 value: "1", name: "a"
}, {
value:  "2", name: "b"
}, {
value:  "3",name: "c"
}, {
 value: "4", name:"d"
}]

Can someone help me figure out how to achieve this.

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

0

const firstArray = ['a','b','c','d'];
const secondArray = [1,2,3,4];

const dict = firstArray.map((i, index) => {
   return {
    value: secondArray[index].toString(),
    name: i
   };     
});
console.log(dict);

about 4 years ago · Juan Pablo Isaza Relatório

0

I think it should be possible to do so

let a = ['a','b','c','d'];
let b = [1,2,3,4];
let tempArray = [];
for(let i = 0;i<a.length;i++){
    
    let tempAttribute = {
        'name' : b[i],
        'value' : a[i]
    }
    tempArray.push(tempAttribute)
}
console.log(tempArray);

Because the subscript of the data is fixed, we can use its subscript to do something, you can also use other looping methods.

about 4 years ago · Juan Pablo Isaza Relatório

0

You can do:

const firstArray = ['a','b','c','d']
const secondArray = [1,2,3,4]

const result = firstArray.map((letter, index) => ({
  name: letter, 
  value: secondArray[index].toString()
}))

console.log(result)

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