Sezz Fess
Sezz Fess

Reputation: 35

JS array recursion

Please help, I have an array of objects, and when create new object I need to check if field "mandalority" not equal "id" and does not exist Objects which "id = 'mandalority of previous\parent objects'" and "mandalority = ID previous\parent objbjects". In this example: if first obj(id5) mandatory "5" - an error, if third obj(id3) mandatory "5" - an error, if fourth obj(id1) mandatory "5" or "3" - an error.

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

Sorry if the question is not clear, this is my first one, this picture will help explain [1]: https://i.sstatic.net/rwR90.png

Upvotes: 0

Views: 74

Answers (2)

Jamie Vangeysel
Jamie Vangeysel

Reputation: 121

Could you describe what end result you are trying to achieve.

Let's say we add object { id: 3, mandatory: null }.
You want this to throw an error since id: 3 exists right?
Object { id: 5, mandatory: 5 } is also a bad example because mandatory equals or is greater than id right?

so the mandatory field has to be an existing id before creation, it cannot point to itself (first check) and the id has to be unique. This extra check to see if the id and mandatory are not creating an infinite loop is wasted since this is not possible to create when we do our first check.

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);

Output:

[
  { 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 }
]

Updated code after remarks:

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)

Updated output:

[
  { 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 }
]

Upvotes: 2

captain4gamer
captain4gamer

Reputation: 1

You should make two variables, one for id and one for mandatory. Each time you create an object you set the id and mandatory as the variables you made and increase the variables by 1 so the next time you make an object it will have a unique I'd.

Upvotes: 0

Related Questions