1

I got an array with some objects. Each object has an id. So when generating a new id, I want to check if an object with this id already exists. If there is an equal id, a new one should be generated.

generateId() {
    var records = store.getRecords(); // get all the objects
    var newId = getNewId(); // calculate a new id

    if (record.id == newId) // id already exists // record.id = id of the object
        newId = generateId(); // generate a new id
    else
        return newId; // return the id
}

getNewId() {
  // generate Id...
}

So how can I check all my records here if (record.id == newId) ? I use JQuery.

6
  • Do you know the strategy of generated Id? Is this a sequence? Commented Aug 4, 2017 at 8:35
  • What is record.id ? Commented Aug 4, 2017 at 8:36
  • No I don't know this. I just tried it out Commented Aug 4, 2017 at 8:36
  • @Sergej record.id is the id of the object Commented Aug 4, 2017 at 8:37
  • Of which object? the record is undefined. And What should be a difference between generateId and getNewId ? Commented Aug 4, 2017 at 8:37

3 Answers 3

2

You can use a simple for loop for simlicity, it might not be efficient if you've got a lot of records obtained. If the structure of the object is the same for all records and assuming the data type of the object value matches the newId variable, this function will serve the purpose.

function DoesExist() {
   for(var i = 0; i < records.length; i++) {
     if(records[i].id == newId)
        return true;
   }

   return false;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Your code should be maintanable. Otherwise there should exist a variables records and newId in a parent/global scope. You should pass them as function input parameters.
@Sergej, This is an example, I am not providing code that one must use. OPs code isn't formatted properly either, hence the reason for my function that I put in place.
1

The way I would go about it is to split my logic into multiple functions, so that I can check any new id against the existing ones. Then, wrapping this inside a loop, I could check generated values until one is found that is not in the array. For example (methods and values added for testing):

function generateId() {
  var records = store.getRecords(); // get all the objects
  var newId; 
  var isUnique = false; 
  while (!isUnique) { // check if unique, repeatedly
    newId = getNewId(); // calculate a new id
    isUnique = checkId(newId);
  }
  return newId; // return the id (is unique)
}
// Check if the id is unique against existing records
function checkId(newId) {
  var records = store.getRecords();
  for (var key in records)
    if (records[key].id == newId)
      return false;
  return true;
}

// Added for testing
function getNewId() {
  return Math.round(Math.random() * 10);
}
var store = {getRecords: function() {return [{id: 1}, {id: 2}, {id: 4}, {id: 6}];}}

// Actual testing
console.log(generateId());

Comments

1

this should work as incremental id generator:

const data = [{id: 1}, {id: 2}, {id: 3}, {id: 4}, {id: 5}, {id: 6}];

const exists = id => data.some(o => o.id === id);

const newId = (start = 0) => {
  const id = ++start;
  
  return exists(id) ? newId(id) : id;
};  


// you can also evaluate to implement some uid logic...
// there isn't much you can do on the client, but, 
// this could also help

const newUID = () => {
  const uid = Math.random().toString(32).substr(2);
  
  return exists(uid) ? newUID() : uid;
}

console.log({
  incrementalID: newId(),
  UID: newUID()
});

7 Comments

so id => data.some(o => o.id === id) should be the important part
also, a recursive approach is the best for this case.
so for my case it should be if (newId => records.some(record => record.id === newId)) right? I get a call stack exception but I think I made an mistake somewhere else
your call stack exception could depend on maximum call stack hit, you might end up on an infinite loop...
It's a maximum callstack exception but I don't get the reason
|

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.