Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

74
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!