var arr = [
{ id: 1, data: 'b' },
{ id: 2, data: 'c' },
{ id: 3, data: 'a' },
{ id: 4, data: 'd' },
]
arr.push({
pId: 4,
id: 5,
data: 'f'
})
arr.push({
nId: 1,
id: 7,
data: 'g'
})
//查找初始的所有数据(未有任何单独插入)
var notPN = arr.filter(ele => ele.pId == undefined && ele.nId == undefined)
//查出有pre
var hasPre = arr.filter(ele => ele.pId != undefined);
//查出有next
var hasNext = arr.filter(ele => ele.nId != undefined)
//pre循环
for (var i = 0; i < hasPre.length; i++) {
var findIndex = notPN.findIndex(ele => ele.id == hasPre[i].pId)
if (findIndex >= 0) {
notPN.splice(findIndex + 1, 0, hasPre[i])
}
}
for (var i = 0; i < hasNext.length; i++) {
var findIndex = notPN.findIndex(ele => ele.id == hasNext[i].nId)
if (findIndex >= 0) {
notPN.splice(findIndex, 0, hasNext[i])
}
}
var tPrint = notPN.map(ele => ele.data)
console.log(JSON.stringify(tPrint))
console