0

I'm trying to make a generate number system where it creates a number from 001 to 999 in order where that number doesn't exist in the json file for example the json file will have the data like this

{
    "001": {
       "some data here": "idk"  
    }
}

where it doesn't add another 001 when it already exists in that json file but in order from 001 to 999 I have tried using Math.random() to generate the number but have no idea how to create the system where it doesn't add another same number

Meme Loke
  • 97
  • 1
  • 9
  • Well, if you want to generate a number from 001 to 999 in order, I'd think a loop would do nicely, perhaps something like `for (let i = 1; i < 1000; i++) {`? And then you'd want to make a zero-padded string out of that `let p = ('000' + i).slice(-3);`. Then check if the file has it, assuming you've does something like `let data = JSON.parse(dataFromFile);`, you could easily do `if (!data[p]) { /* Does not exist */ data[p] = { "add some properties": "here" } }`, or whatever it is you want to do. – Heretic Monkey Apr 27 '21 at 20:39

4 Answers4

0

A loop such as this is one possible solution.

function createNumbers(){  //Numbers 001 - 999. Sequential
  let numbers = [],
      x = 0,
      y = 0,
      z = 0,
      i;
  
  for( i = 0; i < 999; i++ ){
    z++;
    
    if( z > 9 ){
      y++;
      z = 0;
    }
    
    if( y > 9 ){
      x++;
      y = 0;
    }
    
    x = x.toString();
    numbers.push( '<div>' + x + y +  z + '</div>' );
  }
  
  return numbers;
}

document.write( createNumbers() );
body {
  font-family: Arial;
  color: #999;
}
div {
  display: inline-block;
  margin: 0.25rem;
  border-radius: 0.5rem;
  padding: 0.25rem;
  background-color: #ccc;
  color: #444;
}
Lynel Hudson
  • 2,335
  • 1
  • 18
  • 34
0

to generate numbers from 1 to 999 a simple random function would accomplish the task

function getRandomInt() {
  return Math.floor(Math.random() * 999);
}

const generatedNumber = getRandomInt(); // 32

as you want the number to have 3 fixed chars and be a string, you can use padStart for example

const generatedString = `${generatedNumber}`.padStart(3, '0'); // 032

then all you need is to loop through your JSON data and check if that key exists

const JSON = {
  "001": {
    "some data here": "idk"  
  }
};

const allKeys = Object.keys(JSON); // ["001"]
const hasKey = allKeys.find(x => x === generatedString) !== undefined

with all this information, it's a bit easier to then do what you're trying to accomplish

balexandre
  • 73,608
  • 45
  • 233
  • 342
0
let data = {
    "001": {
       "some data here": "idk"  
    }
}

let randUniqueKeyGenerator = (data)=>{
    keys = Object.keys(data);
    found = false;  
    let key;    
    if(keys.includes('100')) return "";
    while(!found){
        key = Math.floor(Math.random()*101).toString()
        key = ('00' + key).slice(-3)
        found = !(keys.includes(key)) 
        // to make key greater than existing keys, uncomment next line
        //if(found) found = !(keys.some(k=>parseInt(k)>parseInt(key)))
  
    }
    return key
}

console.log(randUniqueKeyGenerator(data))

Where data is the object that you need to generate unique keys for.

Syed H
  • 270
  • 1
  • 3
  • 13
  • Thanks! this does all I needed, is the numbers possible to be ascending order from 001 - 099 – Meme Loke Apr 27 '21 at 21:25
  • Yes. Just add this as the last line in your while loop `if(found) found = !(keys.find(k=>parseInt(k)>parseInt(key)))` – Syed H Apr 27 '21 at 23:10
0

You could probably use a simple while loop and compare the Object.keys of your parsed JSON to the current iteration index and include it, if it's not in the keys array.

For example:

const json = '{"001":{"some data here":"idk"}, "003":{"some data here":"idk"}, "005":{"some data here":"idk"}}';
const keys = Object.keys(JSON.parse(json)).map(el => parseInt(el));
const nums = [];
let i = 1;

while (i < 999) {
  if (!keys.includes(i)) {
    nums.push(`${i}`.padStart(3, '0'));
  }

  i++;
}

console.log(nums)
Tom O.
  • 5,730
  • 2
  • 21
  • 35