Home > Software engineering >  Build array of taxonomies by filtering array by array of lowest-level taxonomies
Build array of taxonomies by filtering array by array of lowest-level taxonomies

Time:01-31

There's a fairly big array of all available taxonomies in the shape of
{ id: string, parentId: string, label?: string }[], i.e.

temp1 = [
  { id: '1.1', parentId: '1' },
  { id: '1.1.1', parentId: '1.1' },
  ...
  { id: '1.9.8', parentId: '1.9' },
  ...
  { id: '1.9.8.15', parentId: '1.9.8' },
  ...etc
]

and a list of lowest-possible ID's that we should filter the array above by:

temp2 = ['1.9.8.19', '1.2.3.5', '1.9.8.15', ...etc]

The desired outcome is to have all the temp2 items collected from temp1 along with their parents, grandparents, grand-grandparents, etc, all up to the root, which is '1'.

My take on that was to create a recursive function:

const find = (ids) => ids.forEach(id => {
    const found = temp1.find(item => item.id === id)

    if (!found) return
    
    c.push(found)

    if (found.parentId === '1') return
    
    find([found.parentId])
})

and then run it as follows: find(temp2).

The reason I don't like it is that:

  1. I push to c array which lives outside of the scope of the function
  2. I feel like I'm spawning multiple functions on find(temp2) but then I don't really use arrays anymore to do so: find([found.parentId]
  3. I feel like the function is not performant

Data to play with:

temp2:

["1.9.8.19","1.9.8.3","1.8.1.2","1.8.1.3","1.2.3.5","1.2.3.1","1.9.8.15","1.2.4.1.1","1.9.8.5.1","1.9.8.11.1","1.9.8.21","1.2.3.10.1"]

temp1:

[{"id":"1.1","parentId":"1"},{"id":"1.1.1","parentId":"1.1"},{"id":"1.1.2","parentId":"1.1"},{"id":"1.1.3","parentId":"1.1"},{"id":"1.1.4","parentId":"1.1"},{"id":"1.1.5","parentId":"1.1"},{"id":"1.1.6","parentId":"1.1"},{"id":"1.1.7","parentId":"1.1"},{"id":"1.1.8","parentId":"1.1"},{"id":"1.10","parentId":"1"},{"id":"1.10.1","parentId":"1.10"},{"id":"1.10.10","parentId":"1.10"},{"id":"1.10.11","parentId":"1.10"},{"id":"1.10.12","parentId":"1.10"},{"id":"1.10.13","parentId":"1.10"},{"id":"1.10.14","parentId":"1.10"},{"id":"1.10.15","parentId":"1.10"},{"id":"1.10.16","parentId":"1.10"},{"id":"1.10.17","parentId":"1.10"},{"id":"1.10.18","parentId":"1.10"},{"id":"1.10.19","parentId":"1.10"},{"id":"1.10.2","parentId":"1.10"},{"id":"1.10.20","parentId":"1.10"},{"id":"1.10.21","parentId":"1.10"},{"id":"1.10.22","parentId":"1.10"},{"id":"1.10.23","parentId":"1.10"},{"id":"1.10.24","parentId":"1.10"},{"id":"1.10.25","parentId":"1.10"},{"id":"1.10.26","parentId":"1.10"},{"id":"1.10.3","parentId":"1.10"},{"id":"1.10.4","parentId":"1.10"},{"id":"1.10.5","parentId":"1.10"},{"id":"1.10.6","parentId":"1.10"},{"id":"1.10.7","parentId":"1.10"},{"id":"1.10.8","parentId":"1.10"},{"id":"1.10.9","parentId":"1.10"},{"id":"1.2","parentId":"1"},{"id":"1.2.1","parentId":"1.2"},{"id":"1.2.1.1","parentId":"1.2.1"},{"id":"1.2.1.10","parentId":"1.2.1"},{"id":"1.2.1.11","parentId":"1.2.1"},{"id":"1.2.1.12","parentId":"1.2.1"},{"id":"1.2.1.13","parentId":"1.2.1"},{"id":"1.2.1.14","parentId":"1.2.1"},{"id":"1.2.1.15","parentId":"1.2.1"},{"id":"1.2.1.16","parentId":"1.2.1"},{"id":"1.2.1.17","parentId":"1.2.1"},{"id":"1.2.1.18","parentId":"1.2.1"},{"id":"1.2.1.19","parentId":"1.2.1"},{"id":"1.2.1.2","parentId":"1.2.1"},{"id":"1.2.1.3","parentId":"1.2.1"},{"id":"1.2.1.4","parentId":"1.2.1"},{"id":"1.2.1.5","parentId":"1.2.1"},{"id":"1.2.1.6","parentId":"1.2.1"},{"id":"1.2.1.7","parentId":"1.2.1"},{"id":"1.2.1.8","parentId":"1.2.1"},{"id":"1.2.1.9","parentId":"1.2.1"},{"id":"1.2.2","parentId":"1.2"},{"id":"1.2.2.1","parentId":"1.2.2"},{"id":"1.2.2.2","parentId":"1.2.2"},{"id":"1.2.2.3","parentId":"1.2.2"},{"id":"1.2.2.4","parentId":"1.2.2"},{"id":"1.2.2.5","parentId":"1.2.2"},{"id":"1.2.2.6","parentId":"1.2.2"},{"id":"1.2.2.7","parentId":"1.2.2"},{"id":"1.2.3","parentId":"1.2"},{"id":"1.2.3.1","parentId":"1.2.3"},{"id":"1.2.3.10","parentId":"1.2.3"},{"id":"1.2.3.10.1","parentId":"1.2.3.10"},{"id":"1.2.3.10.2","parentId":"1.2.3.10"},{"id":"1.2.3.10.3","parentId":"1.2.3.10"},{"id":"1.2.3.11","parentId":"1.2.3"},{"id":"1.2.3.12","parentId":"1.2.3"},{"id":"1.2.3.12.1","parentId":"1.2.3.12"},{"id":"1.2.3.12.2","parentId":"1.2.3.12"},{"id":"1.2.3.12.3","parentId":"1.2.3.12"},{"id":"1.2.3.13","parentId":"1.2.3"},{"id":"1.2.3.14","parentId":"1.2.3"},{"id":"1.2.3.15","parentId":"1.2.3"},{"id":"1.2.3.16","parentId":"1.2.3"},{"id":"1.2.3.17","parentId":"1.2.3"},{"id":"1.2.3.18","parentId":"1.2.3"},{"id":"1.2.3.19","parentId":"1.2.3"},{"id":"1.2.3.2","parentId":"1.2.3"},{"id":"1.2.3.20","parentId":"1.2.3"},{"id":"1.2.3.21","parentId":"1.2.3"},{"id":"1.2.3.3","parentId":"1.2.3"},{"id":"1.2.3.4","parentId":"1.2.3"},{"id":"1.2.3.5","parentId":"1.2.3"},{"id":"1.2.3.6","parentId":"1.2.3"},{"id":"1.2.3.7","parentId":"1.2.3"},{"id":"1.2.3.8","parentId":"1.2.3"},{"id":"1.2.3.9","parentId":"1.2.3"},{"id":"1.2.4","parentId":"1.2"},{"id":"1.2.4.1","parentId":"1.2.4"},{"id":"1.2.4.1.1","parentId":"1.2.4.1"},{"id":"1.2.4.1.2","parentId":"1.2.4.1"},{"id":"1.2.4.1.3","parentId":"1.2.4.1"},{"id":"1.2.4.1.4","parentId":"1.2.4.1"},{"id":"1.2.4.2","parentId":"1.2.4"},{"id":"1.2.4.3","parentId":"1.2.4"},{"id":"1.2.4.3.1","parentId":"1.2.4.3"},{"id":"1.2.4.3.2","parentId":"1.2.4.3"},{"id":"1.2.4.3.3","parentId":"1.2.4.3"},{"id":"1.2.4.3.4","parentId":"1.2.4.3"},{"id":"1.2.4.3.5","parentId":"1.2.4.3"},{"id":"1.2.4.4","parentId":"1.2.4"},{"id":"1.3","parentId":"1"},{"id":"1.3.1","parentId":"1.3"},{"id":"1.3.2","parentId":"1.3"},{"id":"1.3.3","parentId":"1.3"},{"id":"1.3.4","parentId":"1.3"},{"id":"1.3.5","parentId":"1.3"},{"id":"1.3.6","parentId":"1.3"},{"id":"1.3.7","parentId":"1.3"},{"id":"1.3.8","parentId":"1.3"},{"id":"1.3.9","parentId":"1.3"},{"id":"1.4","parentId":"1"},{"id":"1.4.1","parentId":"1.4"},{"id":"1.4.2","parentId":"1.4"},{"id":"1.4.3","parentId":"1.4"},{"id":"1.5","parentId":"1"},{"id":"1.5.1","parentId":"1.5"},{"id":"1.5.2","parentId":"1.5"},{"id":"1.5.2.1","parentId":"1.5.2"},{"id":"1.5.2.2","parentId":"1.5.2"},{"id":"1.5.2.3","parentId":"1.5.2"},{"id":"1.5.2.4","parentId":"1.5.2"},{"id":"1.5.2.5","parentId":"1.5.2"},{"id":"1.5.2.6","parentId":"1.5.2"},{"id":"1.5.2.7","parentId":"1.5.2"},{"id":"1.5.2.8","parentId":"1.5.2"},{"id":"1.5.2.9","parentId":"1.5.2"},{"id":"1.5.3","parentId":"1.5"},{"id":"1.5.3.1","parentId":"1.5.3"},{"id":"1.5.3.2","parentId":"1.5.3"},{"id":"1.5.3.2.1","parentId":"1.5.3.2"},{"id":"1.5.3.2.2","parentId":"1.5.3.2"},{"id":"1.5.3.2.3","parentId":"1.5.3.2"},{"id":"1.5.3.3","parentId":"1.5.3"},{"id":"1.5.3.3.1","parentId":"1.5.3.3"},{"id":"1.5.3.3.2","parentId":"1.5.3.3"},{"id":"1.5.3.3.3","parentId":"1.5.3.3"},{"id":"1.5.3.4","parentId":"1.5.3"},{"id":"1.5.4","parentId":"1.5"},{"id":"1.5.4.1","parentId":"1.5.4"},{"id":"1.5.4.2","parentId":"1.5.4"},{"id":"1.5.4.3","parentId":"1.5.4"},{"id":"1.5.4.4","parentId":"1.5.4"},{"id":"1.5.4.5","parentId":"1.5.4"},{"id":"1.5.4.6","parentId":"1.5.4"},{"id":"1.5.4.7","parentId":"1.5.4"},{"id":"1.5.4.8","parentId":"1.5.4"},{"id":"1.6","parentId":"1"},{"id":"1.6.1","parentId":"1.6"},{"id":"1.6.2","parentId":"1.6"},{"id":"1.6.3","parentId":"1.6"},{"id":"1.6.4","parentId":"1.6"},{"id":"1.6.5","parentId":"1.6"},{"id":"1.6.6","parentId":"1.6"},{"id":"1.6.7","parentId":"1.6"},{"id":"1.6.8","parentId":"1.6"},{"id":"1.7","parentId":"1"},{"id":"1.7.1","parentId":"1.7"},{"id":"1.7.10","parentId":"1.7"},{"id":"1.7.11","parentId":"1.7"},{"id":"1.7.12","parentId":"1.7"},{"id":"1.7.13","parentId":"1.7"},{"id":"1.7.14","parentId":"1.7"},{"id":"1.7.15","parentId":"1.7"},{"id":"1.7.16","parentId":"1.7"},{"id":"1.7.2","parentId":"1.7"},{"id":"1.7.3","parentId":"1.7"},{"id":"1.7.4","parentId":"1.7"},{"id":"1.7.5","parentId":"1.7"},{"id":"1.7.6","parentId":"1.7"},{"id":"1.7.7","parentId":"1.7"},{"id":"1.7.8","parentId":"1.7"},{"id":"1.7.9","parentId":"1.7"},{"id":"1.8","parentId":"1"},{"id":"1.8.1","parentId":"1.8"},{"id":"1.8.1.1","parentId":"1.8.1"},{"id":"1.8.1.2","parentId":"1.8.1"},{"id":"1.8.1.3","parentId":"1.8.1"},{"id":"1.8.1.4","parentId":"1.8.1"},{"id":"1.8.1.5","parentId":"1.8.1"},{"id":"1.8.1.6","parentId":"1.8.1"},{"id":"1.8.1.7","parentId":"1.8.1"},{"id":"1.8.1.7.1","parentId":"1.8.1.7"},{"id":"1.8.1.7.2","parentId":"1.8.1.7"},{"id":"1.8.1.7.3","parentId":"1.8.1.7"},{"id":"1.8.2","parentId":"1.8"},{"id":"1.8.2.1","parentId":"1.8.2"},{"id":"1.8.2.2","parentId":"1.8.2"},{"id":"1.8.2.2.1","parentId":"1.8.2.2"},{"id":"1.8.2.2.2","parentId":"1.8.2.2"},{"id":"1.8.2.2.3","parentId":"1.8.2.2"},{"id":"1.8.2.3","parentId":"1.8.2"},{"id":"1.9","parentId":"1"},{"id":"1.9.1","parentId":"1.9"},{"id":"1.9.1.1","parentId":"1.9.1"},{"id":"1.9.1.2","parentId":"1.9.1"},{"id":"1.9.1.3","parentId":"1.9.1"},{"id":"1.9.1.4","parentId":"1.9.1"},{"id":"1.9.1.5","parentId":"1.9.1"},{"id":"1.9.1.6","parentId":"1.9.1"},{"id":"1.9.1.7","parentId":"1.9.1"},{"id":"1.9.1.8","parentId":"1.9.1"},{"id":"1.9.1.9","parentId":"1.9.1"},{"id":"1.9.10","parentId":"1.9"},{"id":"1.9.10.1","parentId":"1.9.10"},{"id":"1.9.10.2","parentId":"1.9.10"},{"id":"1.9.10.3","parentId":"1.9.10"},{"id":"1.9.10.4","parentId":"1.9.10"},{"id":"1.9.10.5","parentId":"1.9.10"},{"id":"1.9.11","parentId":"1.9"},{"id":"1.9.11.1","parentId":"1.9.11"},{"id":"1.9.11.2","parentId":"1.9.11"},{"id":"1.9.11.3","parentId":"1.9.11"},{"id":"1.9.11.4","parentId":"1.9.11"},{"id":"1.9.11.5","parentId":"1.9.11"},{"id":"1.9.11.6","parentId":"1.9.11"},{"id":"1.9.11.7","parentId":"1.9.11"},{"id":"1.9.12","parentId":"1.9"},{"id":"1.9.13","parentId":"1.9"},{"id":"1.9.2","parentId":"1.9"},{"id":"1.9.2.1","parentId":"1.9.2"},{"id":"1.9.2.2","parentId":"1.9.2"},{"id":"1.9.2.3","parentId":"1.9.2"},{"id":"1.9.2.4","parentId":"1.9.2"},{"id":"1.9.2.5","parentId":"1.9.2"},{"id":"1.9.2.6","parentId":"1.9.2"},{"id":"1.9.3","parentId":"1.9"},{"id":"1.9.4","parentId":"1.9"},{"id":"1.9.5","parentId":"1.9"},{"id":"1.9.6","parentId":"1.9"},{"id":"1.9.7","parentId":"1.9"},{"id":"1.9.7.1","parentId":"1.9.7"},{"id":"1.9.7.1.1","parentId":"1.9.7.1"},{"id":"1.9.7.1.2","parentId":"1.9.7.1"},{"id":"1.9.7.1.3","parentId":"1.9.7.1"},{"id":"1.9.7.1.4","parentId":"1.9.7.1"},{"id":"1.9.7.1.5","parentId":"1.9.7.1"},{"id":"1.9.7.2","parentId":"1.9.7"},{"id":"1.9.7.2.1","parentId":"1.9.7.2"},{"id":"1.9.7.2.2","parentId":"1.9.7.2"},{"id":"1.9.7.2.3","parentId":"1.9.7.2"},{"id":"1.9.7.2.4","parentId":"1.9.7.2"},{"id":"1.9.7.2.5","parentId":"1.9.7.2"},{"id":"1.9.7.3","parentId":"1.9.7"},{"id":"1.9.7.3.1","parentId":"1.9.7.3"},{"id":"1.9.7.3.2","parentId":"1.9.7.3"},{"id":"1.9.7.3.3","parentId":"1.9.7.3"},{"id":"1.9.7.3.4","parentId":"1.9.7.3"},{"id":"1.9.7.3.5","parentId":"1.9.7.3"},{"id":"1.9.7.4","parentId":"1.9.7"},{"id":"1.9.8","parentId":"1.9"},{"id":"1.9.8.1","parentId":"1.9.8"},{"id":"1.9.8.1.1","parentId":"1.9.8.1"},{"id":"1.9.8.1.2","parentId":"1.9.8.1"},{"id":"1.9.8.1.3","parentId":"1.9.8.1"},{"id":"1.9.8.1.4","parentId":"1.9.8.1"},{"id":"1.9.8.1.5","parentId":"1.9.8.1"},{"id":"1.9.8.10","parentId":"1.9.8"},{"id":"1.9.8.11","parentId":"1.9.8"},{"id":"1.9.8.11.1","parentId":"1.9.8.11"},{"id":"1.9.8.11.2","parentId":"1.9.8.11"},{"id":"1.9.8.11.3","parentId":"1.9.8.11"},{"id":"1.9.8.12","parentId":"1.9.8"},{"id":"1.9.8.13","parentId":"1.9.8"},{"id":"1.9.8.14","parentId":"1.9.8"},{"id":"1.9.8.15","parentId":"1.9.8"},{"id":"1.9.8.16","parentId":"1.9.8"},{"id":"1.9.8.17","parentId":"1.9.8"},{"id":"1.9.8.18","parentId":"1.9.8"},{"id":"1.9.8.19","parentId":"1.9.8"},{"id":"1.9.8.2","parentId":"1.9.8"},{"id":"1.9.8.20","parentId":"1.9.8"},{"id":"1.9.8.21","parentId":"1.9.8"},{"id":"1.9.8.22","parentId":"1.9.8"},{"id":"1.9.8.23","parentId":"1.9.8"},{"id":"1.9.8.24","parentId":"1.9.8"},{"id":"1.9.8.25","parentId":"1.9.8"},{"id":"1.9.8.25.1","parentId":"1.9.8.25"},{"id":"1.9.8.25.2","parentId":"1.9.8.25"},{"id":"1.9.8.25.3","parentId":"1.9.8.25"},{"id":"1.9.8.26","parentId":"1.9.8"},{"id":"1.9.8.27","parentId":"1.9.8"},{"id":"1.9.8.27.1","parentId":"1.9.8.27"},{"id":"1.9.8.27.2","parentId":"1.9.8.27"},{"id":"1.9.8.3","parentId":"1.9.8"},{"id":"1.9.8.4","parentId":"1.9.8"},{"id":"1.9.8.5","parentId":"1.9.8"},{"id":"1.9.8.5.1","parentId":"1.9.8.5"},{"id":"1.9.8.5.2","parentId":"1.9.8.5"},{"id":"1.9.8.5.3","parentId":"1.9.8.5"},{"id":"1.9.8.6","parentId":"1.9.8"},{"id":"1.9.8.6.1","parentId":"1.9.8.6"},{"id":"1.9.8.6.2","parentId":"1.9.8.6"},{"id":"1.9.8.7","parentId":"1.9.8"},{"id":"1.9.8.8","parentId":"1.9.8"},{"id":"1.9.8.8.1","parentId":"1.9.8.8"},{"id":"1.9.8.8.2","parentId":"1.9.8.8"},{"id":"1.9.8.9","parentId":"1.9.8"},{"id":"1.9.9","parentId":"1.9"},{"id":"1.9.9.1","parentId":"1.9.9"},{"id":"1.9.9.10","parentId":"1.9.9"},{"id":"1.9.9.11","parentId":"1.9.9"},{"id":"1.9.9.12","parentId":"1.9.9"},{"id":"1.9.9.13","parentId":"1.9.9"},{"id":"1.9.9.2","parentId":"1.9.9"},{"id":"1.9.9.3","parentId":"1.9.9"},{"id":"1.9.9.3.1","parentId":"1.9.9.3"},{"id":"1.9.9.3.10","parentId":"1.9.9.3"},{"id":"1.9.9.3.2","parentId":"1.9.9.3"},{"id":"1.9.9.3.3","parentId":"1.9.9.3"},{"id":"1.9.9.3.4","parentId":"1.9.9.3"},{"id":"1.9.9.3.5","parentId":"1.9.9.3"},{"id":"1.9.9.3.6","parentId":"1.9.9.3"},{"id":"1.9.9.3.7","parentId":"1.9.9.3"},{"id":"1.9.9.3.8","parentId":"1.9.9.3"},{"id":"1.9.9.3.9","parentId":"1.9.9.3"},{"id":"1.9.9.4","parentId":"1.9.9"},{"id":"1.9.9.5","parentId":"1.9.9"},{"id":"1.9.9.6","parentId":"1.9.9"},{"id":"1.9.9.7","parentId":"1.9.9"},{"id":"1.9.9.8","parentId":"1.9.9"},{"id":"1.9.9.9","parentId":"1.9.9"}]

Note: temp2 values are guaranteed to be the lowest possible taxonomy levels

CodePudding user response:

You can use Array#flatMap with a recursive function.

let temp2 = ["1.9.8.19","1.9.8.3","1.8.1.2","1.8.1.3","1.2.3.5","1.2.3.1","1.9.8.15","1.2.4.1.1","1.9.8.5.1","1.9.8.11.1","1.9.8.21","1.2.3.10.1"], temp1 = [{"id":"1.1","parentId":"1"},{"id":"1.1.1","parentId":"1.1"},{"id":"1.1.2","parentId":"1.1"},{"id":"1.1.3","parentId":"1.1"},{"id":"1.1.4","parentId":"1.1"},{"id":"1.1.5","parentId":"1.1"},{"id":"1.1.6","parentId":"1.1"},{"id":"1.1.7","parentId":"1.1"},{"id":"1.1.8","parentId":"1.1"},{"id":"1.10","parentId":"1"},{"id":"1.10.1","parentId":"1.10"},{"id":"1.10.10","parentId":"1.10"},{"id":"1.10.11","parentId":"1.10"},{"id":"1.10.12","parentId":"1.10"},{"id":"1.10.13","parentId":"1.10"},{"id":"1.10.14","parentId":"1.10"},{"id":"1.10.15","parentId":"1.10"},{"id":"1.10.16","parentId":"1.10"},{"id":"1.10.17","parentId":"1.10"},{"id":"1.10.18","parentId":"1.10"},{"id":"1.10.19","parentId":"1.10"},{"id":"1.10.2","parentId":"1.10"},{"id":"1.10.20","parentId":"1.10"},{"id":"1.10.21","parentId":"1.10"},{"id":"1.10.22","parentId":"1.10"},{"id":"1.10.23","parentId":"1.10"},{"id":"1.10.24","parentId":"1.10"},{"id":"1.10.25","parentId":"1.10"},{"id":"1.10.26","parentId":"1.10"},{"id":"1.10.3","parentId":"1.10"},{"id":"1.10.4","parentId":"1.10"},{"id":"1.10.5","parentId":"1.10"},{"id":"1.10.6","parentId":"1.10"},{"id":"1.10.7","parentId":"1.10"},{"id":"1.10.8","parentId":"1.10"},{"id":"1.10.9","parentId":"1.10"},{"id":"1.2","parentId":"1"},{"id":"1.2.1","parentId":"1.2"},{"id":"1.2.1.1","parentId":"1.2.1"},{"id":"1.2.1.10","parentId":"1.2.1"},{"id":"1.2.1.11","parentId":"1.2.1"},{"id":"1.2.1.12","parentId":"1.2.1"},{"id":"1.2.1.13","parentId":"1.2.1"},{"id":"1.2.1.14","parentId":"1.2.1"},{"id":"1.2.1.15","parentId":"1.2.1"},{"id":"1.2.1.16","parentId":"1.2.1"},{"id":"1.2.1.17","parentId":"1.2.1"},{"id":"1.2.1.18","parentId":"1.2.1"},{"id":"1.2.1.19","parentId":"1.2.1"},{"id":"1.2.1.2","parentId":"1.2.1"},{"id":"1.2.1.3","parentId":"1.2.1"},{"id":"1.2.1.4","parentId":"1.2.1"},{"id":"1.2.1.5","parentId":"1.2.1"},{"id":"1.2.1.6","parentId":"1.2.1"},{"id":"1.2.1.7","parentId":"1.2.1"},{"id":"1.2.1.8","parentId":"1.2.1"},{"id":"1.2.1.9","parentId":"1.2.1"},{"id":"1.2.2","parentId":"1.2"},{"id":"1.2.2.1","parentId":"1.2.2"},{"id":"1.2.2.2","parentId":"1.2.2"},{"id":"1.2.2.3","parentId":"1.2.2"},{"id":"1.2.2.4","parentId":"1.2.2"},{"id":"1.2.2.5","parentId":"1.2.2"},{"id":"1.2.2.6","parentId":"1.2.2"},{"id":"1.2.2.7","parentId":"1.2.2"},{"id":"1.2.3","parentId":"1.2"},{"id":"1.2.3.1","parentId":"1.2.3"},{"id":"1.2.3.10","parentId":"1.2.3"},{"id":"1.2.3.10.1","parentId":"1.2.3.10"},{"id":"1.2.3.10.2","parentId":"1.2.3.10"},{"id":"1.2.3.10.3","parentId":"1.2.3.10"},{"id":"1.2.3.11","parentId":"1.2.3"},{"id":"1.2.3.12","parentId":"1.2.3"},{"id":"1.2.3.12.1","parentId":"1.2.3.12"},{"id":"1.2.3.12.2","parentId":"1.2.3.12"},{"id":"1.2.3.12.3","parentId":"1.2.3.12"},{"id":"1.2.3.13","parentId":"1.2.3"},{"id":"1.2.3.14","parentId":"1.2.3"},{"id":"1.2.3.15","parentId":"1.2.3"},{"id":"1.2.3.16","parentId":"1.2.3"},{"id":"1.2.3.17","parentId":"1.2.3"},{"id":"1.2.3.18","parentId":"1.2.3"},{"id":"1.2.3.19","parentId":"1.2.3"},{"id":"1.2.3.2","parentId":"1.2.3"},{"id":"1.2.3.20","parentId":"1.2.3"},{"id":"1.2.3.21","parentId":"1.2.3"},{"id":"1.2.3.3","parentId":"1.2.3"},{"id":"1.2.3.4","parentId":"1.2.3"},{"id":"1.2.3.5","parentId":"1.2.3"},{"id":"1.2.3.6","parentId":"1.2.3"},{"id":"1.2.3.7","parentId":"1.2.3"},{"id":"1.2.3.8","parentId":"1.2.3"},{"id":"1.2.3.9","parentId":"1.2.3"},{"id":"1.2.4","parentId":"1.2"},{"id":"1.2.4.1","parentId":"1.2.4"},{"id":"1.2.4.1.1","parentId":"1.2.4.1"},{"id":"1.2.4.1.2","parentId":"1.2.4.1"},{"id":"1.2.4.1.3","parentId":"1.2.4.1"},{"id":"1.2.4.1.4","parentId":"1.2.4.1"},{"id":"1.2.4.2","parentId":"1.2.4"},{"id":"1.2.4.3","parentId":"1.2.4"},{"id":"1.2.4.3.1","parentId":"1.2.4.3"},{"id":"1.2.4.3.2","parentId":"1.2.4.3"},{"id":"1.2.4.3.3","parentId":"1.2.4.3"},{"id":"1.2.4.3.4","parentId":"1.2.4.3"},{"id":"1.2.4.3.5","parentId":"1.2.4.3"},{"id":"1.2.4.4","parentId":"1.2.4"},{"id":"1.3","parentId":"1"},{"id":"1.3.1","parentId":"1.3"},{"id":"1.3.2","parentId":"1.3"},{"id":"1.3.3","parentId":"1.3"},{"id":"1.3.4","parentId":"1.3"},{"id":"1.3.5","parentId":"1.3"},{"id":"1.3.6","parentId":"1.3"},{"id":"1.3.7","parentId":"1.3"},{"id":"1.3.8","parentId":"1.3"},{"id":"1.3.9","parentId":"1.3"},{"id":"1.4","parentId":"1"},{"id":"1.4.1","parentId":"1.4"},{"id":"1.4.2","parentId":"1.4"},{"id":"1.4.3","parentId":"1.4"},{"id":"1.5","parentId":"1"},{"id":"1.5.1","parentId":"1.5"},{"id":"1.5.2","parentId":"1.5"},{"id":"1.5.2.1","parentId":"1.5.2"},{"id":"1.5.2.2","parentId":"1.5.2"},{"id":"1.5.2.3","parentId":"1.5.2"},{"id":"1.5.2.4","parentId":"1.5.2"},{"id":"1.5.2.5","parentId":"1.5.2"},{"id":"1.5.2.6","parentId":"1.5.2"},{"id":"1.5.2.7","parentId":"1.5.2"},{"id":"1.5.2.8","parentId":"1.5.2"},{"id":"1.5.2.9","parentId":"1.5.2"},{"id":"1.5.3","parentId":"1.5"},{"id":"1.5.3.1","parentId":"1.5.3"},{"id":"1.5.3.2","parentId":"1.5.3"},{"id":"1.5.3.2.1","parentId":"1.5.3.2"},{"id":"1.5.3.2.2","parentId":"1.5.3.2"},{"id":"1.5.3.2.3","parentId":"1.5.3.2"},{"id":"1.5.3.3","parentId":"1.5.3"},{"id":"1.5.3.3.1","parentId":"1.5.3.3"},{"id":"1.5.3.3.2","parentId":"1.5.3.3"},{"id":"1.5.3.3.3","parentId":"1.5.3.3"},{"id":"1.5.3.4","parentId":"1.5.3"},{"id":"1.5.4","parentId":"1.5"},{"id":"1.5.4.1","parentId":"1.5.4"},{"id":"1.5.4.2","parentId":"1.5.4"},{"id":"1.5.4.3","parentId":"1.5.4"},{"id":"1.5.4.4","parentId":"1.5.4"},{"id":"1.5.4.5","parentId":"1.5.4"},{"id":"1.5.4.6","parentId":"1.5.4"},{"id":"1.5.4.7","parentId":"1.5.4"},{"id":"1.5.4.8","parentId":"1.5.4"},{"id":"1.6","parentId":"1"},{"id":"1.6.1","parentId":"1.6"},{"id":"1.6.2","parentId":"1.6"},{"id":"1.6.3","parentId":"1.6"},{"id":"1.6.4","parentId":"1.6"},{"id":"1.6.5","parentId":"1.6"},{"id":"1.6.6","parentId":"1.6"},{"id":"1.6.7","parentId":"1.6"},{"id":"1.6.8","parentId":"1.6"},{"id":"1.7","parentId":"1"},{"id":"1.7.1","parentId":"1.7"},{"id":"1.7.10","parentId":"1.7"},{"id":"1.7.11","parentId":"1.7"},{"id":"1.7.12","parentId":"1.7"},{"id":"1.7.13","parentId":"1.7"},{"id":"1.7.14","parentId":"1.7"},{"id":"1.7.15","parentId":"1.7"},{"id":"1.7.16","parentId":"1.7"},{"id":"1.7.2","parentId":"1.7"},{"id":"1.7.3","parentId":"1.7"},{"id":"1.7.4","parentId":"1.7"},{"id":"1.7.5","parentId":"1.7"},{"id":"1.7.6","parentId":"1.7"},{"id":"1.7.7","parentId":"1.7"},{"id":"1.7.8","parentId":"1.7"},{"id":"1.7.9","parentId":"1.7"},{"id":"1.8","parentId":"1"},{"id":"1.8.1","parentId":"1.8"},{"id":"1.8.1.1","parentId":"1.8.1"},{"id":"1.8.1.2","parentId":"1.8.1"},{"id":"1.8.1.3","parentId":"1.8.1"},{"id":"1.8.1.4","parentId":"1.8.1"},{"id":"1.8.1.5","parentId":"1.8.1"},{"id":"1.8.1.6","parentId":"1.8.1"},{"id":"1.8.1.7","parentId":"1.8.1"},{"id":"1.8.1.7.1","parentId":"1.8.1.7"},{"id":"1.8.1.7.2","parentId":"1.8.1.7"},{"id":"1.8.1.7.3","parentId":"1.8.1.7"},{"id":"1.8.2","parentId":"1.8"},{"id":"1.8.2.1","parentId":"1.8.2"},{"id":"1.8.2.2","parentId":"1.8.2"},{"id":"1.8.2.2.1","parentId":"1.8.2.2"},{"id":"1.8.2.2.2","parentId":"1.8.2.2"},{"id":"1.8.2.2.3","parentId":"1.8.2.2"},{"id":"1.8.2.3","parentId":"1.8.2"},{"id":"1.9","parentId":"1"},{"id":"1.9.1","parentId":"1.9"},{"id":"1.9.1.1","parentId":"1.9.1"},{"id":"1.9.1.2","parentId":"1.9.1"},{"id":"1.9.1.3","parentId":"1.9.1"},{"id":"1.9.1.4","parentId":"1.9.1"},{"id":"1.9.1.5","parentId":"1.9.1"},{"id":"1.9.1.6","parentId":"1.9.1"},{"id":"1.9.1.7","parentId":"1.9.1"},{"id":"1.9.1.8","parentId":"1.9.1"},{"id":"1.9.1.9","parentId":"1.9.1"},{"id":"1.9.10","parentId":"1.9"},{"id":"1.9.10.1","parentId":"1.9.10"},{"id":"1.9.10.2","parentId":"1.9.10"},{"id":"1.9.10.3","parentId":"1.9.10"},{"id":"1.9.10.4","parentId":"1.9.10"},{"id":"1.9.10.5","parentId":"1.9.10"},{"id":"1.9.11","parentId":"1.9"},{"id":"1.9.11.1","parentId":"1.9.11"},{"id":"1.9.11.2","parentId":"1.9.11"},{"id":"1.9.11.3","parentId":"1.9.11"},{"id":"1.9.11.4","parentId":"1.9.11"},{"id":"1.9.11.5","parentId":"1.9.11"},{"id":"1.9.11.6","parentId":"1.9.11"},{"id":"1.9.11.7","parentId":"1.9.11"},{"id":"1.9.12","parentId":"1.9"},{"id":"1.9.13","parentId":"1.9"},{"id":"1.9.2","parentId":"1.9"},{"id":"1.9.2.1","parentId":"1.9.2"},{"id":"1.9.2.2","parentId":"1.9.2"},{"id":"1.9.2.3","parentId":"1.9.2"},{"id":"1.9.2.4","parentId":"1.9.2"},{"id":"1.9.2.5","parentId":"1.9.2"},{"id":"1.9.2.6","parentId":"1.9.2"},{"id":"1.9.3","parentId":"1.9"},{"id":"1.9.4","parentId":"1.9"},{"id":"1.9.5","parentId":"1.9"},{"id":"1.9.6","parentId":"1.9"},{"id":"1.9.7","parentId":"1.9"},{"id":"1.9.7.1","parentId":"1.9.7"},{"id":"1.9.7.1.1","parentId":"1.9.7.1"},{"id":"1.9.7.1.2","parentId":"1.9.7.1"},{"id":"1.9.7.1.3","parentId":"1.9.7.1"},{"id":"1.9.7.1.4","parentId":"1.9.7.1"},{"id":"1.9.7.1.5","parentId":"1.9.7.1"},{"id":"1.9.7.2","parentId":"1.9.7"},{"id":"1.9.7.2.1","parentId":"1.9.7.2"},{"id":"1.9.7.2.2","parentId":"1.9.7.2"},{"id":"1.9.7.2.3","parentId":"1.9.7.2"},{"id":"1.9.7.2.4","parentId":"1.9.7.2"},{"id":"1.9.7.2.5","parentId":"1.9.7.2"},{"id":"1.9.7.3","parentId":"1.9.7"},{"id":"1.9.7.3.1","parentId":"1.9.7.3"},{"id":"1.9.7.3.2","parentId":"1.9.7.3"},{"id":"1.9.7.3.3","parentId":"1.9.7.3"},{"id":"1.9.7.3.4","parentId":"1.9.7.3"},{"id":"1.9.7.3.5","parentId":"1.9.7.3"},{"id":"1.9.7.4","parentId":"1.9.7"},{"id":"1.9.8","parentId":"1.9"},{"id":"1.9.8.1","parentId":"1.9.8"},{"id":"1.9.8.1.1","parentId":"1.9.8.1"},{"id":"1.9.8.1.2","parentId":"1.9.8.1"},{"id":"1.9.8.1.3","parentId":"1.9.8.1"},{"id":"1.9.8.1.4","parentId":"1.9.8.1"},{"id":"1.9.8.1.5","parentId":"1.9.8.1"},{"id":"1.9.8.10","parentId":"1.9.8"},{"id":"1.9.8.11","parentId":"1.9.8"},{"id":"1.9.8.11.1","parentId":"1.9.8.11"},{"id":"1.9.8.11.2","parentId":"1.9.8.11"},{"id":"1.9.8.11.3","parentId":"1.9.8.11"},{"id":"1.9.8.12","parentId":"1.9.8"},{"id":"1.9.8.13","parentId":"1.9.8"},{"id":"1.9.8.14","parentId":"1.9.8"},{"id":"1.9.8.15","parentId":"1.9.8"},{"id":"1.9.8.16","parentId":"1.9.8"},{"id":"1.9.8.17","parentId":"1.9.8"},{"id":"1.9.8.18","parentId":"1.9.8"},{"id":"1.9.8.19","parentId":"1.9.8"},{"id":"1.9.8.2","parentId":"1.9.8"},{"id":"1.9.8.20","parentId":"1.9.8"},{"id":"1.9.8.21","parentId":"1.9.8"},{"id":"1.9.8.22","parentId":"1.9.8"},{"id":"1.9.8.23","parentId":"1.9.8"},{"id":"1.9.8.24","parentId":"1.9.8"},{"id":"1.9.8.25","parentId":"1.9.8"},{"id":"1.9.8.25.1","parentId":"1.9.8.25"},{"id":"1.9.8.25.2","parentId":"1.9.8.25"},{"id":"1.9.8.25.3","parentId":"1.9.8.25"},{"id":"1.9.8.26","parentId":"1.9.8"},{"id":"1.9.8.27","parentId":"1.9.8"},{"id":"1.9.8.27.1","parentId":"1.9.8.27"},{"id":"1.9.8.27.2","parentId":"1.9.8.27"},{"id":"1.9.8.3","parentId":"1.9.8"},{"id":"1.9.8.4","parentId":"1.9.8"},{"id":"1.9.8.5","parentId":"1.9.8"},{"id":"1.9.8.5.1","parentId":"1.9.8.5"},{"id":"1.9.8.5.2","parentId":"1.9.8.5"},{"id":"1.9.8.5.3","parentId":"1.9.8.5"},{"id":"1.9.8.6","parentId":"1.9.8"},{"id":"1.9.8.6.1","parentId":"1.9.8.6"},{"id":"1.9.8.6.2","parentId":"1.9.8.6"},{"id":"1.9.8.7","parentId":"1.9.8"},{"id":"1.9.8.8","parentId":"1.9.8"},{"id":"1.9.8.8.1","parentId":"1.9.8.8"},{"id":"1.9.8.8.2","parentId":"1.9.8.8"},{"id":"1.9.8.9","parentId":"1.9.8"},{"id":"1.9.9","parentId":"1.9"},{"id":"1.9.9.1","parentId":"1.9.9"},{"id":"1.9.9.10","parentId":"1.9.9"},{"id":"1.9.9.11","parentId":"1.9.9"},{"id":"1.9.9.12","parentId":"1.9.9"},{"id":"1.9.9.13","parentId":"1.9.9"},{"id":"1.9.9.2","parentId":"1.9.9"},{"id":"1.9.9.3","parentId":"1.9.9"},{"id":"1.9.9.3.1","parentId":"1.9.9.3"},{"id":"1.9.9.3.10","parentId":"1.9.9.3"},{"id":"1.9.9.3.2","parentId":"1.9.9.3"},{"id":"1.9.9.3.3","parentId":"1.9.9.3"},{"id":"1.9.9.3.4","parentId":"1.9.9.3"},{"id":"1.9.9.3.5","parentId":"1.9.9.3"},{"id":"1.9.9.3.6","parentId":"1.9.9.3"},{"id":"1.9.9.3.7","parentId":"1.9.9.3"},{"id":"1.9.9.3.8","parentId":"1.9.9.3"},{"id":"1.9.9.3.9","parentId":"1.9.9.3"},{"id":"1.9.9.4","parentId":"1.9.9"},{"id":"1.9.9.5","parentId":"1.9.9"},{"id":"1.9.9.6","parentId":"1.9.9"},{"id":"1.9.9.7","parentId":"1.9.9"},{"id":"1.9.9.8","parentId":"1.9.9"},{"id":"1.9.9.9","parentId":"1.9.9"}];
let res = [...new Set(temp2.flatMap(function get(id) {
    const obj = temp1.find(y => y.id === id);
    return [obj, ...obj.parentId !== '1' && get(obj.parentId) || []];
}))];
console.log(res);

CodePudding user response:

const a = [{"id":"1.1","parentId":"1"},{"id":"1.1.1","parentId":"1.1"},{"id":"1.1.2","parentId":"1.1"},{"id":"1.1.3","parentId":"1.1"},{"id":"1.1.4","parentId":"1.1"},{"id":"1.1.5","parentId":"1.1"},{"id":"1.1.6","parentId":"1.1"},{"id":"1.1.7","parentId":"1.1"},{"id":"1.1.8","parentId":"1.1"},{"id":"1.10","parentId":"1"},{"id":"1.10.1","parentId":"1.10"},{"id":"1.10.10","parentId":"1.10"},{"id":"1.10.11","parentId":"1.10"},{"id":"1.10.12","parentId":"1.10"},{"id":"1.10.13","parentId":"1.10"},{"id":"1.10.14","parentId":"1.10"},{"id":"1.10.15","parentId":"1.10"},{"id":"1.10.16","parentId":"1.10"},{"id":"1.10.17","parentId":"1.10"},{"id":"1.10.18","parentId":"1.10"},{"id":"1.10.19","parentId":"1.10"},{"id":"1.10.2","parentId":"1.10"},{"id":"1.10.20","parentId":"1.10"},{"id":"1.10.21","parentId":"1.10"},{"id":"1.10.22","parentId":"1.10"},{"id":"1.10.23","parentId":"1.10"},{"id":"1.10.24","parentId":"1.10"},{"id":"1.10.25","parentId":"1.10"},{"id":"1.10.26","parentId":"1.10"},{"id":"1.10.3","parentId":"1.10"},{"id":"1.10.4","parentId":"1.10"},{"id":"1.10.5","parentId":"1.10"},{"id":"1.10.6","parentId":"1.10"},{"id":"1.10.7","parentId":"1.10"},{"id":"1.10.8","parentId":"1.10"},{"id":"1.10.9","parentId":"1.10"},{"id":"1.2","parentId":"1"},{"id":"1.2.1","parentId":"1.2"},{"id":"1.2.1.1","parentId":"1.2.1"},{"id":"1.2.1.10","parentId":"1.2.1"},{"id":"1.2.1.11","parentId":"1.2.1"},{"id":"1.2.1.12","parentId":"1.2.1"},{"id":"1.2.1.13","parentId":"1.2.1"},{"id":"1.2.1.14","parentId":"1.2.1"},{"id":"1.2.1.15","parentId":"1.2.1"},{"id":"1.2.1.16","parentId":"1.2.1"},{"id":"1.2.1.17","parentId":"1.2.1"},{"id":"1.2.1.18","parentId":"1.2.1"},{"id":"1.2.1.19","parentId":"1.2.1"},{"id":"1.2.1.2","parentId":"1.2.1"},{"id":"1.2.1.3","parentId":"1.2.1"},{"id":"1.2.1.4","parentId":"1.2.1"},{"id":"1.2.1.5","parentId":"1.2.1"},{"id":"1.2.1.6","parentId":"1.2.1"},{"id":"1.2.1.7","parentId":"1.2.1"},{"id":"1.2.1.8","parentId":"1.2.1"},{"id":"1.2.1.9","parentId":"1.2.1"},{"id":"1.2.2","parentId":"1.2"},{"id":"1.2.2.1","parentId":"1.2.2"},{"id":"1.2.2.2","parentId":"1.2.2"},{"id":"1.2.2.3","parentId":"1.2.2"},{"id":"1.2.2.4","parentId":"1.2.2"},{"id":"1.2.2.5","parentId":"1.2.2"},{"id":"1.2.2.6","parentId":"1.2.2"},{"id":"1.2.2.7","parentId":"1.2.2"},{"id":"1.2.3","parentId":"1.2"},{"id":"1.2.3.1","parentId":"1.2.3"},{"id":"1.2.3.10","parentId":"1.2.3"},{"id":"1.2.3.10.1","parentId":"1.2.3.10"},{"id":"1.2.3.10.2","parentId":"1.2.3.10"},{"id":"1.2.3.10.3","parentId":"1.2.3.10"},{"id":"1.2.3.11","parentId":"1.2.3"},{"id":"1.2.3.12","parentId":"1.2.3"},{"id":"1.2.3.12.1","parentId":"1.2.3.12"},{"id":"1.2.3.12.2","parentId":"1.2.3.12"},{"id":"1.2.3.12.3","parentId":"1.2.3.12"},{"id":"1.2.3.13","parentId":"1.2.3"},{"id":"1.2.3.14","parentId":"1.2.3"},{"id":"1.2.3.15","parentId":"1.2.3"},{"id":"1.2.3.16","parentId":"1.2.3"},{"id":"1.2.3.17","parentId":"1.2.3"},{"id":"1.2.3.18","parentId":"1.2.3"},{"id":"1.2.3.19","parentId":"1.2.3"},{"id":"1.2.3.2","parentId":"1.2.3"},{"id":"1.2.3.20","parentId":"1.2.3"},{"id":"1.2.3.21","parentId":"1.2.3"},{"id":"1.2.3.3","parentId":"1.2.3"},{"id":"1.2.3.4","parentId":"1.2.3"},{"id":"1.2.3.5","parentId":"1.2.3"},{"id":"1.2.3.6","parentId":"1.2.3"},{"id":"1.2.3.7","parentId":"1.2.3"},{"id":"1.2.3.8","parentId":"1.2.3"},{"id":"1.2.3.9","parentId":"1.2.3"},{"id":"1.2.4","parentId":"1.2"},{"id":"1.2.4.1","parentId":"1.2.4"},{"id":"1.2.4.1.1","parentId":"1.2.4.1"},{"id":"1.2.4.1.2","parentId":"1.2.4.1"},{"id":"1.2.4.1.3","parentId":"1.2.4.1"},{"id":"1.2.4.1.4","parentId":"1.2.4.1"},{"id":"1.2.4.2","parentId":"1.2.4"},{"id":"1.2.4.3","parentId":"1.2.4"},{"id":"1.2.4.3.1","parentId":"1.2.4.3"},{"id":"1.2.4.3.2","parentId":"1.2.4.3"},{"id":"1.2.4.3.3","parentId":"1.2.4.3"},{"id":"1.2.4.3.4","parentId":"1.2.4.3"},{"id":"1.2.4.3.5","parentId":"1.2.4.3"},{"id":"1.2.4.4","parentId":"1.2.4"},{"id":"1.3","parentId":"1"},{"id":"1.3.1","parentId":"1.3"},{"id":"1.3.2","parentId":"1.3"},{"id":"1.3.3","parentId":"1.3"},{"id":"1.3.4","parentId":"1.3"},{"id":"1.3.5","parentId":"1.3"},{"id":"1.3.6","parentId":"1.3"},{"id":"1.3.7","parentId":"1.3"},{"id":"1.3.8","parentId":"1.3"},{"id":"1.3.9","parentId":"1.3"},{"id":"1.4","parentId":"1"},{"id":"1.4.1","parentId":"1.4"},{"id":"1.4.2","parentId":"1.4"},{"id":"1.4.3","parentId":"1.4"},{"id":"1.5","parentId":"1"},{"id":"1.5.1","parentId":"1.5"},{"id":"1.5.2","parentId":"1.5"},{"id":"1.5.2.1","parentId":"1.5.2"},{"id":"1.5.2.2","parentId":"1.5.2"},{"id":"1.5.2.3","parentId":"1.5.2"},{"id":"1.5.2.4","parentId":"1.5.2"},{"id":"1.5.2.5","parentId":"1.5.2"},{"id":"1.5.2.6","parentId":"1.5.2"},{"id":"1.5.2.7","parentId":"1.5.2"},{"id":"1.5.2.8","parentId":"1.5.2"},{"id":"1.5.2.9","parentId":"1.5.2"},{"id":"1.5.3","parentId":"1.5"},{"id":"1.5.3.1","parentId":"1.5.3"},{"id":"1.5.3.2","parentId":"1.5.3"},{"id":"1.5.3.2.1","parentId":"1.5.3.2"},{"id":"1.5.3.2.2","parentId":"1.5.3.2"},{"id":"1.5.3.2.3","parentId":"1.5.3.2"},{"id":"1.5.3.3","parentId":"1.5.3"},{"id":"1.5.3.3.1","parentId":"1.5.3.3"},{"id":"1.5.3.3.2","parentId":"1.5.3.3"},{"id":"1.5.3.3.3","parentId":"1.5.3.3"},{"id":"1.5.3.4","parentId":"1.5.3"},{"id":"1.5.4","parentId":"1.5"},{"id":"1.5.4.1","parentId":"1.5.4"},{"id":"1.5.4.2","parentId":"1.5.4"},{"id":"1.5.4.3","parentId":"1.5.4"},{"id":"1.5.4.4","parentId":"1.5.4"},{"id":"1.5.4.5","parentId":"1.5.4"},{"id":"1.5.4.6","parentId":"1.5.4"},{"id":"1.5.4.7","parentId":"1.5.4"},{"id":"1.5.4.8","parentId":"1.5.4"},{"id":"1.6","parentId":"1"},{"id":"1.6.1","parentId":"1.6"},{"id":"1.6.2","parentId":"1.6"},{"id":"1.6.3","parentId":"1.6"},{"id":"1.6.4","parentId":"1.6"},{"id":"1.6.5","parentId":"1.6"},{"id":"1.6.6","parentId":"1.6"},{"id":"1.6.7","parentId":"1.6"},{"id":"1.6.8","parentId":"1.6"},{"id":"1.7","parentId":"1"},{"id":"1.7.1","parentId":"1.7"},{"id":"1.7.10","parentId":"1.7"},{"id":"1.7.11","parentId":"1.7"},{"id":"1.7.12","parentId":"1.7"},{"id":"1.7.13","parentId":"1.7"},{"id":"1.7.14","parentId":"1.7"},{"id":"1.7.15","parentId":"1.7"},{"id":"1.7.16","parentId":"1.7"},{"id":"1.7.2","parentId":"1.7"},{"id":"1.7.3","parentId":"1.7"},{"id":"1.7.4","parentId":"1.7"},{"id":"1.7.5","parentId":"1.7"},{"id":"1.7.6","parentId":"1.7"},{"id":"1.7.7","parentId":"1.7"},{"id":"1.7.8","parentId":"1.7"},{"id":"1.7.9","parentId":"1.7"},{"id":"1.8","parentId":"1"},{"id":"1.8.1","parentId":"1.8"},{"id":"1.8.1.1","parentId":"1.8.1"},{"id":"1.8.1.2","parentId":"1.8.1"},{"id":"1.8.1.3","parentId":"1.8.1"},{"id":"1.8.1.4","parentId":"1.8.1"},{"id":"1.8.1.5","parentId":"1.8.1"},{"id":"1.8.1.6","parentId":"1.8.1"},{"id":"1.8.1.7","parentId":"1.8.1"},{"id":"1.8.1.7.1","parentId":"1.8.1.7"},{"id":"1.8.1.7.2","parentId":"1.8.1.7"},{"id":"1.8.1.7.3","parentId":"1.8.1.7"},{"id":"1.8.2","parentId":"1.8"},{"id":"1.8.2.1","parentId":"1.8.2"},{"id":"1.8.2.2","parentId":"1.8.2"},{"id":"1.8.2.2.1","parentId":"1.8.2.2"},{"id":"1.8.2.2.2","parentId":"1.8.2.2"},{"id":"1.8.2.2.3","parentId":"1.8.2.2"},{"id":"1.8.2.3","parentId":"1.8.2"},{"id":"1.9","parentId":"1"},{"id":"1.9.1","parentId":"1.9"},{"id":"1.9.1.1","parentId":"1.9.1"},{"id":"1.9.1.2","parentId":"1.9.1"},{"id":"1.9.1.3","parentId":"1.9.1"},{"id":"1.9.1.4","parentId":"1.9.1"},{"id":"1.9.1.5","parentId":"1.9.1"},{"id":"1.9.1.6","parentId":"1.9.1"},{"id":"1.9.1.7","parentId":"1.9.1"},{"id":"1.9.1.8","parentId":"1.9.1"},{"id":"1.9.1.9","parentId":"1.9.1"},{"id":"1.9.10","parentId":"1.9"},{"id":"1.9.10.1","parentId":"1.9.10"},{"id":"1.9.10.2","parentId":"1.9.10"},{"id":"1.9.10.3","parentId":"1.9.10"},{"id":"1.9.10.4","parentId":"1.9.10"},{"id":"1.9.10.5","parentId":"1.9.10"},{"id":"1.9.11","parentId":"1.9"},{"id":"1.9.11.1","parentId":"1.9.11"},{"id":"1.9.11.2","parentId":"1.9.11"},{"id":"1.9.11.3","parentId":"1.9.11"},{"id":"1.9.11.4","parentId":"1.9.11"},{"id":"1.9.11.5","parentId":"1.9.11"},{"id":"1.9.11.6","parentId":"1.9.11"},{"id":"1.9.11.7","parentId":"1.9.11"},{"id":"1.9.12","parentId":"1.9"},{"id":"1.9.13","parentId":"1.9"},{"id":"1.9.2","parentId":"1.9"},{"id":"1.9.2.1","parentId":"1.9.2"},{"id":"1.9.2.2","parentId":"1.9.2"},{"id":"1.9.2.3","parentId":"1.9.2"},{"id":"1.9.2.4","parentId":"1.9.2"},{"id":"1.9.2.5","parentId":"1.9.2"},{"id":"1.9.2.6","parentId":"1.9.2"},{"id":"1.9.3","parentId":"1.9"},{"id":"1.9.4","parentId":"1.9"},{"id":"1.9.5","parentId":"1.9"},{"id":"1.9.6","parentId":"1.9"},{"id":"1.9.7","parentId":"1.9"},{"id":"1.9.7.1","parentId":"1.9.7"},{"id":"1.9.7.1.1","parentId":"1.9.7.1"},{"id":"1.9.7.1.2","parentId":"1.9.7.1"},{"id":"1.9.7.1.3","parentId":"1.9.7.1"},{"id":"1.9.7.1.4","parentId":"1.9.7.1"},{"id":"1.9.7.1.5","parentId":"1.9.7.1"},{"id":"1.9.7.2","parentId":"1.9.7"},{"id":"1.9.7.2.1","parentId":"1.9.7.2"},{"id":"1.9.7.2.2","parentId":"1.9.7.2"},{"id":"1.9.7.2.3","parentId":"1.9.7.2"},{"id":"1.9.7.2.4","parentId":"1.9.7.2"},{"id":"1.9.7.2.5","parentId":"1.9.7.2"},{"id":"1.9.7.3","parentId":"1.9.7"},{"id":"1.9.7.3.1","parentId":"1.9.7.3"},{"id":"1.9.7.3.2","parentId":"1.9.7.3"},{"id":"1.9.7.3.3","parentId":"1.9.7.3"},{"id":"1.9.7.3.4","parentId":"1.9.7.3"},{"id":"1.9.7.3.5","parentId":"1.9.7.3"},{"id":"1.9.7.4","parentId":"1.9.7"},{"id":"1.9.8","parentId":"1.9"},{"id":"1.9.8.1","parentId":"1.9.8"},{"id":"1.9.8.1.1","parentId":"1.9.8.1"},{"id":"1.9.8.1.2","parentId":"1.9.8.1"},{"id":"1.9.8.1.3","parentId":"1.9.8.1"},{"id":"1.9.8.1.4","parentId":"1.9.8.1"},{"id":"1.9.8.1.5","parentId":"1.9.8.1"},{"id":"1.9.8.10","parentId":"1.9.8"},{"id":"1.9.8.11","parentId":"1.9.8"},{"id":"1.9.8.11.1","parentId":"1.9.8.11"},{"id":"1.9.8.11.2","parentId":"1.9.8.11"},{"id":"1.9.8.11.3","parentId":"1.9.8.11"},{"id":"1.9.8.12","parentId":"1.9.8"},{"id":"1.9.8.13","parentId":"1.9.8"},{"id":"1.9.8.14","parentId":"1.9.8"},{"id":"1.9.8.15","parentId":"1.9.8"},{"id":"1.9.8.16","parentId":"1.9.8"},{"id":"1.9.8.17","parentId":"1.9.8"},{"id":"1.9.8.18","parentId":"1.9.8"},{"id":"1.9.8.19","parentId":"1.9.8"},{"id":"1.9.8.2","parentId":"1.9.8"},{"id":"1.9.8.20","parentId":"1.9.8"},{"id":"1.9.8.21","parentId":"1.9.8"},{"id":"1.9.8.22","parentId":"1.9.8"},{"id":"1.9.8.23","parentId":"1.9.8"},{"id":"1.9.8.24","parentId":"1.9.8"},{"id":"1.9.8.25","parentId":"1.9.8"},{"id":"1.9.8.25.1","parentId":"1.9.8.25"},{"id":"1.9.8.25.2","parentId":"1.9.8.25"},{"id":"1.9.8.25.3","parentId":"1.9.8.25"},{"id":"1.9.8.26","parentId":"1.9.8"},{"id":"1.9.8.27","parentId":"1.9.8"},{"id":"1.9.8.27.1","parentId":"1.9.8.27"},{"id":"1.9.8.27.2","parentId":"1.9.8.27"},{"id":"1.9.8.3","parentId":"1.9.8"},{"id":"1.9.8.4","parentId":"1.9.8"},{"id":"1.9.8.5","parentId":"1.9.8"},{"id":"1.9.8.5.1","parentId":"1.9.8.5"},{"id":"1.9.8.5.2","parentId":"1.9.8.5"},{"id":"1.9.8.5.3","parentId":"1.9.8.5"},{"id":"1.9.8.6","parentId":"1.9.8"},{"id":"1.9.8.6.1","parentId":"1.9.8.6"},{"id":"1.9.8.6.2","parentId":"1.9.8.6"},{"id":"1.9.8.7","parentId":"1.9.8"},{"id":"1.9.8.8","parentId":"1.9.8"},{"id":"1.9.8.8.1","parentId":"1.9.8.8"},{"id":"1.9.8.8.2","parentId":"1.9.8.8"},{"id":"1.9.8.9","parentId":"1.9.8"},{"id":"1.9.9","parentId":"1.9"},{"id":"1.9.9.1","parentId":"1.9.9"},{"id":"1.9.9.10","parentId":"1.9.9"},{"id":"1.9.9.11","parentId":"1.9.9"},{"id":"1.9.9.12","parentId":"1.9.9"},{"id":"1.9.9.13","parentId":"1.9.9"},{"id":"1.9.9.2","parentId":"1.9.9"},{"id":"1.9.9.3","parentId":"1.9.9"},{"id":"1.9.9.3.1","parentId":"1.9.9.3"},{"id":"1.9.9.3.10","parentId":"1.9.9.3"},{"id":"1.9.9.3.2","parentId":"1.9.9.3"},{"id":"1.9.9.3.3","parentId":"1.9.9.3"},{"id":"1.9.9.3.4","parentId":"1.9.9.3"},{"id":"1.9.9.3.5","parentId":"1.9.9.3"},{"id":"1.9.9.3.6","parentId":"1.9.9.3"},{"id":"1.9.9.3.7","parentId":"1.9.9.3"},{"id":"1.9.9.3.8","parentId":"1.9.9.3"},{"id":"1.9.9.3.9","parentId":"1.9.9.3"},{"id":"1.9.9.4","parentId":"1.9.9"},{"id":"1.9.9.5","parentId":"1.9.9"},{"id":"1.9.9.6","parentId":"1.9.9"},{"id":"1.9.9.7","parentId":"1.9.9"},{"id":"1.9.9.8","parentId":"1.9.9"},{"id":"1.9.9.9","parentId":"1.9.9"}]

const b = ["1.9.8.19","1.9.8.3","1.8.1.2","1.8.1.3","1.2.3.5","1.2.3.1","1.9.8.15","1.2.4.1.1","1.9.8.5.1","1.9.8.11.1","1.9.8.21","1.2.3.10.1"]

const f = (a,b,c) => [...new Set([
  ...c=a.filter(({id})=>b.includes(id)),
  ...c.length?f(a,c.map(i=>i.parentId)):[]])]

console.log(f(a,b))

  • Related