我有一个看起来像这样的对象:
let dataObject = {firstName: "Mike", employeeId: "1020"}
我想用每个属性创建一个数组,如下所示:
let newArray = [
{operator:"sum", value: "Mike", property: "firstName", type: "string"},
{operator:"sum", value: "1020", property: "employeeId", type:"string"}
];
到目前为止,我知道了:
let dataObject = {firstName: "Mike", employeeId: "1020"}
let newArray = Object.keys(dataObject).map((key)=>
`{${key}}:${dataObject[key]}`
);
console.log(newArray);
但是我仍然无法获取数组中的对象。如何创建此数组?
答案 0 :(得分:1)
let dataObject = {firstName: "Mike", employeeId: "1020"};
var newArr = Object
.keys(dataObject)
.map((key) => {
return { operate: 'sum', value: dataObject[key], property: key, type: typeof dataObject[key]}
});
console.log(newArr);
答案 1 :(得分:0)
.map
中entries
的{{1}},因此您可以基于原始对象中每个键值对的键和值创建另一个数组:
dataObject
答案 2 :(得分:-1)
您需要将数组映射到新对象,而不是映射到表示一个对象的字符串。
FontWeight