如何访问或循环访问 objects 的嵌套数组,有时可以是无限的,或者用户在 nodejs 中的 jSON 数组 objects 中不断添加?

How to access or loop through the nested array of objects which can be infinite sometimes or keep adding by the user in jSON array objects in nodejs?

我有一个 JSON object,它有一个嵌套数组 children。所以基本上我有一个可以不断扩展的家谱,这意味着可以添加 children。我想通过循环访问这个家谱中的每个 object。我怎样才能做到这一点?我想不开。或者还有其他方法可以实现这个目标吗?

exports familyHead = {
      name: 'Shan',
      type:String,
      generation:101,
      children : [
        {
          name: 'Chit',
          type:String,
          gender:'Male',
          generation:102,
          children :[
            {
              name: 'Dritha',
              type:String,
              gender:'Female',
              generation:103,
              children:[
                {
                  name: 'Yodhan',
                  type:String,
                  gender:'Male',
                  generation:104
                }
              ]
            },
            {
              name: 'Tritha',
              type:String,
              gender:'Female',
              generation:103
            },
            {
              name: 'Vritha',
              type:String,
              gender:'Male',
              generation:103
            }
          ]
        },
        {
          name: 'Ish',
          type:String,
          gender:'Male',
          generation:102
        },
        {
          name: 'Vich',
          type:String,
          gender:'Male',
          generation:102,
          children:[
            {
              name: 'Vila',
              type:String,
              gender:'Female',
              generation:103
          },
          {
            name: 'Chika',
            type:String,
            gender:'Female',
            generation:103
        }
        ]
        },
        {
          name: 'Aras',
          type:String,
          gender:'Male',
          generation:102,
          children:[
            {
              name: 'Jinki',
              type:String,
              gender:'Female',
              generation:103,
              children:[
                {
                  name: 'Laki',
                  type:String,
                  gender:'Male',
                  generation:104
                },
                {
                  name: 'Lavanya',
                  type:String,
                  gender:'Female',
                  generation:104
                }
              ]
          },
          {
            name: 'Ahit',
            type:String,
            gender:'Male',
            generation:103
        }
          ]
        },
        {
          name: 'Satya',
          type:String,
          gender:'Female',
          generation:102,
          children:[
            {
              name: 'Asva',
              type:String,
              gender:'Male',
              generation:103,
              children:[
                {
                  name: 'Vasa',
                  type:String,
                  gender:'Male',
                  generation:104
                }
              ]
            },
            {
              name: 'Vyas',
              type:String,
              gender:'Male',
              generation:103,
              children:[
                {
                  name: 'Kriya',
                  type:String,
                  gender:'Male',
                  generation:104
                },
                {
                  name: 'Krithi',
                  type:String,
                  gender:'Male',
                  generation:104
                }
              ]
            },
            {
              name: 'Atya',
              type:String,
              gender:'Female',
              generation:103
            }
          ]
        }
      ]
    }

您可以使用递归访问家庭中的每个人。如果你想显示每个家庭成员的名字,例如,你可以显示这个人的名字,然后递归调用每个 child.

的函数

const familyHead = { name: 'Shan', generation: 101, children: [{ name: 'Chit', gender: 'Male', generation: 102, children: [{ name: 'Dritha', gender: 'Female', generation: 103, children: [{ name: 'Yodhan', gender: 'Male', generation: 104 } ]}, { name: 'Tritha', gender: 'Female', generation: 103}, { name: 'Vritha', gender: 'Male', generation: 103 } ]}, { name: 'Ish', gender: 'Male', generation: 102}, { name: 'Vich', gender: 'Male', generation: 102, children: [{ name: 'Vila', gender: 'Female', generation: 103}, { name: 'Chika', gender: 'Female', generation: 103 } ]}, { name: 'Aras', gender: 'Male', generation: 102, children: [{ name: 'Jinki', gender: 'Female', generation: 103, children: [{ name: 'Laki', gender: 'Male', generation: 104}, { name: 'Lavanya', gender: 'Female', generation: 104 } ]}, { name: 'Ahit', gender: 'Male', generation: 103 } ]}, { name: 'Satya', type: String, gender: 'Female', generation: 102, children: [{ name: 'Asva', gender: 'Male', generation: 103, children: [{ name: 'Vasa', gender: 'Male', generation: 104 } ]}, { name: 'Vyas', gender: 'Male', generation: 103, children: [{ name: 'Kriya', gender: 'Male', generation: 104}, { name: 'Krithi', gender: 'Male', generation: 104 } ]}, { name: 'Atya', gender: 'Female', generation: 103 } ]} ]};

const display = (person) => {
  console.log(person.name)

  if (person.children) {
    person.children.forEach(display)
  }
}

display(familyHead)

如果您想要 return 所有姓名的数组,请将此人的姓名添加到列表中并为每个 child.

递归调用该函数

const familyHead = { name: 'Shan', generation: 101, children: [{ name: 'Chit', gender: 'Male', generation: 102, children: [{ name: 'Dritha', gender: 'Female', generation: 103, children: [{ name: 'Yodhan', gender: 'Male', generation: 104 } ]}, { name: 'Tritha', gender: 'Female', generation: 103}, { name: 'Vritha', gender: 'Male', generation: 103 } ]}, { name: 'Ish', gender: 'Male', generation: 102}, { name: 'Vich', gender: 'Male', generation: 102, children: [{ name: 'Vila', gender: 'Female', generation: 103}, { name: 'Chika', gender: 'Female', generation: 103 } ]}, { name: 'Aras', gender: 'Male', generation: 102, children: [{ name: 'Jinki', gender: 'Female', generation: 103, children: [{ name: 'Laki', gender: 'Male', generation: 104}, { name: 'Lavanya', gender: 'Female', generation: 104 } ]}, { name: 'Ahit', gender: 'Male', generation: 103 } ]}, { name: 'Satya', type: String, gender: 'Female', generation: 102, children: [{ name: 'Asva', gender: 'Male', generation: 103, children: [{ name: 'Vasa', gender: 'Male', generation: 104 } ]}, { name: 'Vyas', gender: 'Male', generation: 103, children: [{ name: 'Kriya', gender: 'Male', generation: 104}, { name: 'Krithi', gender: 'Male', generation: 104 } ]}, { name: 'Atya', gender: 'Female', generation: 103 } ]} ]}

const getNames = (person) => {
  if (!person.children) {
    return [person.name]
  }

  return [person.name, ...person.children.flatMap(getNames)]
}

console.log(getNames(familyHead))