Cleaning combination Operation of js Array objects

the following array is generated by operation

var newarr = [{"id":"sd123","component":"start"},
{"id":"sd1a3","component":"ordinary"},
...,{"id":"sdop3","component":"end"}]

I thought it was easy to find the start group, get the target, and then loop through the source"s id, to match source"s id,push to the empty array according to the id, of source.

but in the actual coding, the ordinary for loop stops only looking for the first one. In addition, there is internal nesting and a recursion is added. As soon as it is tried, it becomes a dead loop

.
Mar.13,2021

ideas for reference


arclistnoderecursion

function sortStart(arr) {
  var origin = arr
  var result = []
  var start =origin.filter(item => item.source.component === 'start')[0]
  result.push(start.source)
  recursion(origin, start, result)
  return result
}
function recursion(origin, start, result) {
  if (start.source.component === 'end') {
      result.push(start.source)
      return result
  }
  start = origin.filter(item => start.target.id === item.source.id)[0]
  result.push(start.source)
  if (start.target.component === 'end') {
    result.push(start.target)
    return result
  } else {
    recursion(origin, start, result)
  }
}

[ { id: 'sd123', component: 'start' },
  { id: 'sd1a3', component: 'ordinary' },
  { id: 'sdbf3', component: 'ordinary' },
  { id: 'sd3bn', component: 'block' },
  { id: 'er12a', component: 'ordinary' },
  { id: 'sdop3', component: 'end' } ]
Menu