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' } ]
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1eb6322-25bd.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1eb6322-25bd.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?