0

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

4
  • mandalority? someone's been on disney+ too much :D Commented Jan 18, 2021 at 17:07
  • 1
    i do not understand the intention as described. can you please share you attempt at solving the problem and explain where you are stuck? Commented Jan 18, 2021 at 17:12
  • "mandalority" - its some kind of "pass", in this example Obj whith id=3 available after Obj whith id=1. User can change "mandalority" after Obj is created. But if he change "mandalority" of fourth obj(id1) to "3", both will become inaccessible because 1 need 3 but 3 need 1. Commented Jan 18, 2021 at 17:46
  • @NVRM: If you comment is meant as some "recusion (n): see recursion"-style joke, it's cute but probably not a helpful comment. If not, then I think you linked to the wrong question. Commented Jan 18, 2021 at 19:09

2 Answers 2

2

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 }
]
Sign up to request clarification or add additional context in comments.

4 Comments

Sorry if the question is not clear, this is my first one, this picture will help explain [1]: i.sstatic.net/rwR90.png .ID always unique, we can only change mandalority.
When I drew this I saw the exit condition of recursion and "end result Im trying to achieve" - "mandalority == "null" in the end of chain"
So you already have checks in place for an unique id I assume, we now also now check if the mandatory field has a reference to an existing id. do you need mandatory: null to be the last entry? or can you have multiple mandatory: null entries?
in the array many obj may have mandatory=null, in the "tree" like as in the picture above -- at least one
0

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.

1 Comment

"mandatory" is one of the existing ID's, its some kind of "pass"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.