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.
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);
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.
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)