Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var obj = [{
- id: 1,
- name: 'abc',
- child: [{
- id: 2,
- name: 'xyz',
- child: [{
- id: 3,
- name: 'abcd',
- child: []
- }, {
- id: 3,
- name: 'abcd',
- child: []
- }]
- }]
- }, {
- id: 100,
- name: 'dfsdfdsf',
- child: [{
- id: 101,
- name: 'xsdfsfyz',
- child: [{
- id: 102,
- name: 'sdfsfsdfsd',
- child: []
- }, {
- id: 103,
- name: 'sdfsdfsfd',
- child: []
- }]
- }]
- }];
- function recursion(input, arr = []) {
- if (!input.length) {
- return arr;
- }
- input.forEach(e => {
- arr.push({
- id: e.id,
- name: e.name
- });
- recursion(e.child, arr);
- });
- return arr;
- }
- console.log(recursion(obj, []));
Add Comment
Please, Sign In to add comment