抱歉,如果标题有误导性,我不确定如何准确地描述我正在寻找的内容。
这段代码返回一个对象数组,这不是我想要的,因为我需要在我的代码中稍后使用
我希望这有意义,我感觉离我想要的很近,只是缺少一些小东西,但我尝试让它工作的每种方式都变成了语法错误。
I have an array result
which I want to turn into multiple objects where each objects property is the field Name Test
I need to keep my result.map method as I use it to merge result
with data
result = [{
"Name Test": "Yellow",
Count: 124,
},
{
"Name Test": "Black",
Count: 124,
},
{
"Name Test": "Blue",
Count: 124,
}
];
data = [{
"Name Test": "Yellow",
pop: 1
},
{
"Name Test": "Black",
pop: 1
},
{
"Name Test": "Blue",
pop: 1
}
];
result = result.map((obj1, index) => {
const obj2 = data[index];
return {
[obj1["Name Test"].toUpperCase()]: {
Count: obj1.Count,
pop: obj2.pop,
}
};
});
console.log(JSON.stringify(result))
result ["YELLOW"]
。 因此,我需要结果以没有数组的这种格式呈现。{
"YELLOW":{
"Count":124,
"pop":1
},
"BLACK":{
"Count":124,
"pop":1
},
"BLUE":{
"Count":124,
"pop":1
}
}
我希望这有意义,我感觉离我想要的很近,只是缺少一些小东西,但我尝试让它工作的每种方式都变成了语法错误。