0

So I'm trying to generate an array that will have multiple objects, one of them with another array. Each object should have its own unique id, but when using for loop, each batch has objects with the same repeated ids. using i variable or even adding random number value seems to be not working. Is this even achievable in javascript?

Test.js

const testFunc = () => {
  let resultArray = []
  let testObj = {}
  let testArray = []

  const rn = () => {
    return ~~(Math.random() * 1000)
  }

  for (let i = 0; i < 3; i++) {
    testObj = { id: rn() }
    testArray.push(testObj)
    testObj = {}
  }

  for (let i = 0; i < 3; i++) {
    resultArray.push(testArray)
  }

  console.log(resultArray)
  return resultArray
}

Expected result:

[
  [
    { id: 859, text: 0 },
    { id: 232, text: 1 },
    { id: 240, text: 2 },
    { id: 638, text: 3 },
    { id: 393, text: 4 }
  ],
  [
    { id: 681, text: 0 },
    { id: 384, text: 1 },
    { id: 413, text: 2 },
    { id: 721, text: 3 },
    { id: 985, text: 4 }
  ],
  [
    { id: 348, text: 0 },
    { id: 911, text: 1 },
    { id: 255, text: 2 },
    { id: 816, text: 3 },
    { id: 797, text: 4 }
  ]
]

Result I get:

[
  [
    { id: 859, text: 0 },
    { id: 232, text: 1 },
    { id: 240, text: 2 },
    { id: 638, text: 3 },
    { id: 393, text: 4 }
  ],
  [
    { id: 859, text: 0 },
    { id: 232, text: 1 },
    { id: 240, text: 2 },
    { id: 638, text: 3 },
    { id: 393, text: 4 }
  ],
  [
    { id: 859, text: 0 },
    { id: 232, text: 1 },
    { id: 240, text: 2 },
    { id: 638, text: 3 },
    { id: 393, text: 4 }
  ]
]
3
  • You'll have a lot of trouble with id collisions if you're picking a random number from 0 to 999. It's called the birthday paradox. In your case, you only need 38 items before you have 50% chance of having 2 of the same ids.Either keep a counter, or use uuid() from the npm uuid package. Commented May 7, 2021 at 23:34
  • @CharlesBamford Thanks for the comment! I tried uuid() as well, but the result was exactly the same - except that it's not in numbers only. Commented May 8, 2021 at 5:36
  • That's because of the problems the answers solve. Commented May 10, 2021 at 16:38

2 Answers 2

1

You push the testArray thrice to the resultArray. Instead generate testArray in a loop, push it to the resultArray, and add the items:

const testFunc = () => {
  const resultArray = []

  const rn = () => {
    return ~~(Math.random() * 1000)
  }

  for (let i = 0; i < 3; i++) {
    const testArray = []
    resultArray.push(testArray)
    
    for (let i = 0; i < 3; i++) {
      const testObj = { id: rn() }
      testArray.push(testObj)
    }
  }

  return resultArray
}

console.log(testFunc())

Sign up to request clarification or add additional context in comments.

1 Comment

So I was just pushing three identical items. No wonder they all have the same ids. Thank you!
0

You're only ever creating a single sub-array. The line

let testArray = []

only runs once, and you do

resultArray.push(testArray)

3 times - so in the result, all sub-arrays in resultArray refer to the same testArray.

Create a new array explicitly instead, and use a nested loop so that there are 3x3 iterations total:

let resultArray = []
let testObj = {}

const rn = () => {
  return ~~(Math.random() * 1000)
}

for (let i = 0; i < 3; i++) {
  const testArray = []
  for (let i = 0; i < 3; i++) {
    const testObj = {
      id: rn()
    }
    testArray.push(testObj)
  }
  resultArray.push(testArray)
}


console.log(resultArray)

1 Comment

Thank you for the comment! Your explanation cleared my head a lot

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.