如何在键值对对象中添加文本
How to add text in key value pair Object
我有两个数组数据,我可以将它们转换成一个键值对对象。
firstArray: [] = ['a','b','c','d']
secondArray: [] = [1,2,3,4]
将两个数组转换为键值对的代码:
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"
}]
期望的输出:
[{
value: "1", name: "a"
}, {
value: "2", name: "b"
}, {
value: "3",name: "c"
}, {
value: "4", name:"d"
}]
谁能帮我弄清楚如何实现这个。
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);
我觉得应该可以的
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);
因为数据的下标是固定的,我们可以利用它的下标来做一些事情,也可以使用其他的循环方式。
你可以这样做:
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)
我有两个数组数据,我可以将它们转换成一个键值对对象。
firstArray: [] = ['a','b','c','d']
secondArray: [] = [1,2,3,4]
将两个数组转换为键值对的代码:
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"
}]
期望的输出:
[{
value: "1", name: "a"
}, {
value: "2", name: "b"
}, {
value: "3",name: "c"
}, {
value: "4", name:"d"
}]
谁能帮我弄清楚如何实现这个。
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);
我觉得应该可以的
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);
因为数据的下标是固定的,我们可以利用它的下标来做一些事情,也可以使用其他的循环方式。
你可以这样做:
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)