Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width">
- <title>JS Bin</title>
- </head>
- <body>
- <script id="jsbin-javascript">
- let flatArray = [];
- let obj = {
- name: 'a',
- parent: {
- father: {
- name: 'af',
- parent: {
- father: {
- name: 'aff'
- },
- mother: {
- name: 'afm'
- }
- }
- },
- mother: {
- name: 'am',
- parent: {
- father: {
- name: 'amf'
- },
- mother: {
- name: 'amm'
- }
- }
- }
- }
- }
- function getPerson(str, person){
- let x = {...person};
- console.log('=======', str, person)
- str += " "+person.name;
- if( !person.parent ){
- flatArray.push(str);
- return;
- }
- getPerson(str, person.parent.father);
- getPerson(str, person.parent.mother);
- return;
- }
- getPerson("", obj);
- console.log(' flat array is ', flatArray);
- </script>
- <script id="jsbin-source-javascript" type="text/javascript">let flatArray = [];
- let obj = {
- name: 'a',
- parent: {
- father: {
- name: 'af',
- parent: {
- father: {
- name: 'aff'
- },
- mother: {
- name: 'afm'
- }
- }
- },
- mother: {
- name: 'am',
- parent: {
- father: {
- name: 'amf'
- },
- mother: {
- name: 'amm'
- }
- }
- }
- }
- }
- function getPerson(str, person){
- let x = {...person};
- console.log('=======', str, person)
- str += " "+person.name;
- if( !person.parent ){
- flatArray.push(str);
- return;
- }
- getPerson(str, person.parent.father);
- getPerson(str, person.parent.mother);
- return;
- }
- getPerson("", obj);
- console.log(' flat array is ', flatArray);
- </script></body>
- </html>
Add Comment
Please, Sign In to add comment