Как преобразовать структуру объекта в массив?
Все привет, подскажите, пожалуйста, как преобразовать структуру объекта в массив?
Из:
const obj = {
one: ['1', '2', '3'],
two: ['4', '5', '6'],
three: ['7', '8', '9'],
four: ['10', '11', '12'],
}
Необходимо получить:
const arr = [
{
one: ['1', '2', '3']
},
{
two: ['4', '5', '6']
},
{
three: ['7', '8', '9']
},
{
four: ['10', '11', '12']
}
]
|
darktowerk56c,
<script>
const obj = {
one: ['1', '2', '3'],
two: ['4', '5', '6'],
three: ['7', '8', '9'],
four: ['10', '11', '12'],
}
const arr = Object.keys(obj).map(key => ({[key] : obj[key]}));
document.write(JSON.stringify(arr, "", 4))
</script>
|
Спасибо.
|
darktowerk56c,
или так
<script>
const obj = {
one: ['1', '2', '3'],
two: ['4', '5', '6'],
three: ['7', '8', '9'],
four: ['10', '11', '12'],
}
const arr = Object.entries(obj).map(([key, value]) => ({[key] : value}));
document.write(JSON.stringify(arr, "", 4))
</script>
|
| Часовой пояс GMT +3, время: 14:29. |