Задать вопрос
@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'}]}
]
  • Вопрос задан
  • 263 просмотра
Подписаться 1 Простой 2 комментария
Помогут разобраться в теме Все курсы
  • Нетология
    Веб-разработчик с нуля: профессия с выбором специализации
    14 месяцев
    Далее
  • Академия Эдюсон
    Fullstack-разработчик на JavaScript + ИИ
    11 месяцев
    Далее
  • ProductStar × РБК
    Профессия: Инженер по тестированию + ИИ
    6 месяцев
    Далее
Решения вопроса 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
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

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

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