如何修改{}的key为object的值Angular 8
How to modify the key of {} to be a value of object Angular 8
我希望重新安排键成为对象元素的一部分。
{
123: [ {geoLong: 323, geoLat:4234}, {geoLong: 325, geoLat: 3422} ],
349: [ {geoLong: 768, geoLat:456},
{geoLong: 7234, geoLat: 590},
{geoLong: 7234, geoLat: 590} ],
958: [ {geoLong: 643, geoLat:290},
{geoLong: 567, geoLat: 378}],
...
}
在我压平对象之前得到类似下面的结果。
{
123: [ {geoLong: 323, geoLat:4234, id: 123},
{geoLong: 325, geoLat: 3422, id: 123} ],
349: [ {geoLong: 768, geoLat:456, id: 349},
{geoLong: 7234, geoLat: 590, id: 349},
{geoLong: 7234, geoLat: 590, id: 349} ],
958: [ {geoLong: 643, geoLat:290, id: 958},
{geoLong: 567, geoLat: 378, id: 958}],
...
}
您可以使用 Object.entries 方法遍历对象。
obj = {
123: [ {geoLong: 323, geoLat:4234}, {geoLong: 325, geoLat: 3422} ],
349: [ {geoLong: 768, geoLat:456},
{geoLong: 7234, geoLat: 590},
{geoLong: 7234, geoLat: 590} ],
958: [ {geoLong: 643, geoLat:290},
{geoLong: 567, geoLat: 378}],
}
Object.entries(obj).forEach(([key, values]) => {
values.forEach(item => item.id = (+key));
})
结果:
{
"123": [
{
"geoLong": 323,
"geoLat": 4234,
"id": 123
},
{
"geoLong": 325,
"geoLat": 3422,
"id": 123
}
],
"349": [
{
"geoLong": 768,
"geoLat": 456,
"id": 349
},
{
"geoLong": 7234,
"geoLat": 590,
"id": 349
},
{
"geoLong": 7234,
"geoLat": 590,
"id": 349
}
],
"958": [
{
"geoLong": 643,
"geoLat": 290,
"id": 958
},
{
"geoLong": 567,
"geoLat": 378,
"id": 958
}
]
}
您可以使用 Object.keys() and map() 来做到这一点。
var data = { 123: [ {geoLong: 323, geoLat:4234}, {geoLong: 325, geoLat: 3422} ], 349: [ {geoLong: 768, geoLat:456}, {geoLong: 7234, geoLat: 590}, {geoLong: 7234, geoLat: 590} ], 958: [ {geoLong: 643, geoLat:290}, {geoLong: 567, geoLat: 378}] };
Object.keys(data).forEach(key => {
// Filter 'undefined' and 'null' values.
data[key] = data[key].filter(item => item);
data[key] = data[key].map(item => {
item.id = key;
return item;
});
});
console.log(data);
我希望重新安排键成为对象元素的一部分。
{
123: [ {geoLong: 323, geoLat:4234}, {geoLong: 325, geoLat: 3422} ],
349: [ {geoLong: 768, geoLat:456},
{geoLong: 7234, geoLat: 590},
{geoLong: 7234, geoLat: 590} ],
958: [ {geoLong: 643, geoLat:290},
{geoLong: 567, geoLat: 378}],
...
}
在我压平对象之前得到类似下面的结果。
{
123: [ {geoLong: 323, geoLat:4234, id: 123},
{geoLong: 325, geoLat: 3422, id: 123} ],
349: [ {geoLong: 768, geoLat:456, id: 349},
{geoLong: 7234, geoLat: 590, id: 349},
{geoLong: 7234, geoLat: 590, id: 349} ],
958: [ {geoLong: 643, geoLat:290, id: 958},
{geoLong: 567, geoLat: 378, id: 958}],
...
}
您可以使用 Object.entries 方法遍历对象。
obj = {
123: [ {geoLong: 323, geoLat:4234}, {geoLong: 325, geoLat: 3422} ],
349: [ {geoLong: 768, geoLat:456},
{geoLong: 7234, geoLat: 590},
{geoLong: 7234, geoLat: 590} ],
958: [ {geoLong: 643, geoLat:290},
{geoLong: 567, geoLat: 378}],
}
Object.entries(obj).forEach(([key, values]) => {
values.forEach(item => item.id = (+key));
})
结果:
{
"123": [
{
"geoLong": 323,
"geoLat": 4234,
"id": 123
},
{
"geoLong": 325,
"geoLat": 3422,
"id": 123
}
],
"349": [
{
"geoLong": 768,
"geoLat": 456,
"id": 349
},
{
"geoLong": 7234,
"geoLat": 590,
"id": 349
},
{
"geoLong": 7234,
"geoLat": 590,
"id": 349
}
],
"958": [
{
"geoLong": 643,
"geoLat": 290,
"id": 958
},
{
"geoLong": 567,
"geoLat": 378,
"id": 958
}
]
}
您可以使用 Object.keys() and map() 来做到这一点。
var data = { 123: [ {geoLong: 323, geoLat:4234}, {geoLong: 325, geoLat: 3422} ], 349: [ {geoLong: 768, geoLat:456}, {geoLong: 7234, geoLat: 590}, {geoLong: 7234, geoLat: 590} ], 958: [ {geoLong: 643, geoLat:290}, {geoLong: 567, geoLat: 378}] };
Object.keys(data).forEach(key => {
// Filter 'undefined' and 'null' values.
data[key] = data[key].filter(item => item);
data[key] = data[key].map(item => {
item.id = key;
return item;
});
});
console.log(data);