如何使用javascript中另一个对象的对值创建对象
{
firstObject:{
{
"version":"1000",
"issue":"issue1"
},
{
"version":"1001",
"issue":"issue2"
},
{
"version":"1000",
"issue":"issue3"
}
}
}
{
newObject:{
"1000":["issue1", "issue3"],
"1001":["issue2"]
}
}
答案 0 :(得分:0)
您可以尝试
let input = {
firstObject:[
{
"version":"1000",
"issue":"issue1"
},
{
"version":"1001",
"issue":"issue2"
},
{
"version":"1000",
"issue":"issue3"
}
]
}
function createNewObject( input ){
let output= {};
input.firstObject.map(( item ) => {
if( !output[ item.version ]){
output[ item.version ] =[]
}
output[ item.version ].push( item.issue )
})
return({
newObject: output
})
}
console.log( createNewObject( input ))
答案 1 :(得分:0)
您的输入不是valid
JSON。 firstObject
应该是数组而不是对象。
演示
var obj = {
"firstObject": [{
"version": "1000",
"issue": "issue1"
},
{
"version": "1001",
"issue": "issue2"
},
{
"version": "1000",
"issue": "issue3"
}
]
};
var newObject = {};
obj.firstObject.map((item) => {
if( !newObject[ item.version ]){
newObject[item.version] = [];
}
newObject[item.version].push(item.issue);
});
console.log({ newObject });