JS数组递归



请帮助,我有一个对象数组,当创建新对象时,我需要检查字段是否';mandalority"不等于"id";并且不存在"id = '先前父对象的男权'"one_answers"mandalority = ID先前的父对象"。在这个例子中:if first obj(id5) mandatory "5"-错误,If third obj(id3) mandatory "5"-错误,如果第四个obj(id1)必选"5"或"3"-错误。

[{
id: 5,
mandatory: 3
},
{
id: 4,
mandatory: 3
},
{
id: 3,
mandatory: 1
},
{
id: 1,
mandatory: null
},
]

对不起,如果你的问题不是很清楚,这是我的第一个问题,这张图片将有助于解释[1]: https://i.stack.imgur.com/rwR90.png

你能描述一下你想要达到的最终结果吗?

假设我们添加对象{id: 3, mandatory: null}。
既然id: 3存在,你希望它抛出一个错误,对吗?
Object {id: 5, mandatory: 5}也是一个不好的例子,因为mandatory等于或大于id,对吗?

因此,必填字段必须是创建前已存在的id,它不能指向自身(第一次检查)并且id必须是唯一的。这个额外的检查来检查id和mandatory是否没有创建一个无限循环是浪费的,因为这是不可能在我们第一次检查时创建的。

const arr = [{
id: 5,
mandatory: 3
},
{
id: 4,
mandatory: 3
},
{
id: 3,
mandatory: 1
},
{
id: 1,
mandatory: null
}]
function addItem(id, mandatory) {
const entry = {
id,
mandatory
}
// check if id does not already exist, and the mandatory feld is an existing id
if (arr.some(e => e.id === id)) {
console.log(`The following entry ${JSON.stringify(entry)} contains a duplicate id field: ${id}`)
return false
}
// check if mandatory field is an existing id
if (!arr.some(e => e.id === mandatory)) {
console.log(`The following entry ${JSON.stringify(entry)} contains an invalid mandatory field: ${mandatory}`)
return false
}
console.log('pusing entry ', entry)
arr.push(entry)
return true
}
console.log(arr);
addItem(5, 3) // duplicate id
addItem(6, 7) // invalid mandatory > id
addItem(6, 4) // correct
console.log(arr);

输出:

[
{ id: 5, mandatory: 3 },
{ id: 4, mandatory: 3 },
{ id: 3, mandatory: 1 },
{ id: 1, mandatory: null }
]
The following entry {"id":5,"mandatory":3} contains a duplicate id field: 5
The following entry {"id":6,"mandatory":7} contains an invalid mandatory field: 7
pusing entry  { id: 6, mandatory: 4 }
[
{ id: 5, mandatory: 3 },
{ id: 4, mandatory: 3 },
{ id: 3, mandatory: 1 },
{ id: 1, mandatory: null },
{ id: 6, mandatory: 4 }
]

注释后的更新代码:

const arr = [{
id: 5,
mandatory: 3
},
{
id: 4,
mandatory: 3
},
{
id: 3,
mandatory: 1
},
{
id: 1,
mandatory: null
}]
function updateMandatory(id, mandatory) {
// get entry form arr
const entry = arr.find(e => e.id === id)
if (entry) {
// check if mandatory field is an existing id
let mandatoryEntry = arr.find(e => e.id === mandatory)
if (!mandatoryEntry) {
console.log(`The following entry ${JSON.stringify(entry)} contains an invalid mandatory field: ${mandatory}`)
return false
}
// check recursion
var endSearch = mandatoryEntry.mandatory === null
while (!endSearch) {
if (mandatoryEntry.mandatory === id) {
console.log(`Loop detected for entry ${JSON.stringify(mandatoryEntry)}`)
return false
}
mandatoryEntry = arr.find(e => e.id === mandatoryEntry.mandatory)
var endSearch = mandatoryEntry.mandatory === null
}
entry.mandatory = mandatory
return true
}
console.log(`The entry with id: ${id} does not exist!`)
return false
}
function addItem() {
const lastId = Math.max(...arr.map(e => e.id))
const entry = {
id: lastId + 1,
mandatory: null
}
console.log('pusing entry ', entry)
arr.unshift(entry)
return true
}
console.log(arr)
addItem()
updateMandatory(6, 5)
updateMandatory(5, 6)
console.log(arr)

更新输出:

[
{ id: 5, mandatory: 3 },
{ id: 4, mandatory: 3 },
{ id: 3, mandatory: 1 },
{ id: 1, mandatory: null }
]
pusing entry  { id: 6, mandatory: null }
Loop detected for entry {"id":6,"mandatory":5}
[
{ id: 6, mandatory: 5 },
{ id: 5, mandatory: 3 },
{ id: 4, mandatory: 3 },
{ id: 3, mandatory: 1 },
{ id: 1, mandatory: null }
]

您应该创建两个变量,一个用于id,另一个用于强制。每次创建对象时,您都将id和mandatory设置为所创建的变量,并将变量增加1,以便下次创建对象时它将具有唯一的I'd。

最新更新