Задать вопрос
@AskJs

Как создать новый массив объектов из старого?

Есть массив :
[
{test: 'as', test1: 'asss', test2:[{hello: 'a', hell: 'b'}, {hello: 'a', hell: 'b'}]},
{test: 'as', test1: 'asss', test2:[{hello: 'a', hell: 'b'}, {hello: 'a', hell: 'b'}]},
{test: 'as', test1: 'asss', test2:[{hello: 'a', hell: 'b'}, {hello: 'a', hell: 'b'}]}
]

Как из него сделать массив типа:
[
{test: 'as', children: [{hello: 'a'}]},
{test: 'as', children: [{hello: 'a'}]},
{test: 'as', children: [{hello: 'a'}]}
]
  • Вопрос задан
  • 261 просмотр
Подписаться 1 Простой 2 комментария
Помогут разобраться в теме Все курсы
  • Skillbox
    JavaScript
    3 месяца
    Далее
  • Яндекс Практикум
    Фронтенд-разработчик расширенный
    13 месяцев
    Далее
  • Академия Eduson
    Fullstack-разработчик на JavaScript
    11 месяцев
    Далее
Решения вопроса 1
@vaajnur
битриксоид
var a = [
{test: 'as', test1: 'asss', test2:[{hello: 'a', hell: 'b'}, {hello: 'a', hell: 'b'}]},
{test: 'as', test1: 'asss', test2:[{hello: 'a', hell: 'b'}, {hello: 'a', hell: 'b'}]},
{test: 'as', test1: 'asss', test2:[{hello: 'a', hell: 'b'}, {hello: 'a', hell: 'b'}]}
]

var b = []
a.forEach(function(item){
	b.push({test: item.test, children: [ { hello: item.test2[0].hello } ] })

})

b
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы