Issue
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.
Solution
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);
Answered By - Craig Wayne
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.