A. L
A. L

Reputation: 12679

javascript - object referencing does not reference as expected

So I have the following code:

async function runQuery(input)
{
  return input
}

async function createAccounts()
{
  return [1, 2]
}



(async function ()
{
  var
    creator1_ids =
        {},
    creator2_ids =
        {}

  let all_accounts =
    await createAccounts(2)

  const tables =
    [
      "list",
      "display_list",
      "map"
    ]

  for (let index in all_accounts)
  {
    let req =
      {
        session:
        {
          account:
            null
        }
      }
    req.session.account =
      all_accounts[index]
    let the_ids =
        index === 0
            ? creator1_ids
            : creator2_ids

    // Create item
    let result =
      await runQuery()

    the_ids.item = 
      1

    for (let table of tables)
    {
      result =
          await runQuery(table)
      the_ids[table] = 
          result
    }
  }
  
  console.log(creator1_ids)
  console.log(creator2_ids)  
})()

Now javascript uses objects as references (so if you assign another variable to an object, changing either variables would change the overall object), however this doesn't seem to be the case here.

creator1_ids remains empty, while only creator2_ids is filled. I'm expecting creator1_ids to be filled similarly.

But this works.

async function runQuery(input)
{
  return input
}

async function createAccounts()
{
  return [1, 2]
}



(async function ()
{

    var
      creator1_ids =
          {},
      creator2_ids =
          {}
    let all_accounts =
        await createAccounts(2)

    const tables =
        [
            "list",
            "display_list",
            "map"
        ]

    async function generate(the_ids, account)
    {
       let req =
        {
          session:
          {
            account:
              null
          }
        }

      // Create item
      let result =
        await runQuery()

      the_ids.item = 
        1

      for (let table of tables)
      {
        result =
            await runQuery(table)
        the_ids[table] = 
            result + account
      }
    }

    await generate(creator1_ids, all_accounts[0])
    await generate(creator2_ids, all_accounts[1])

    console.log(creator1_ids)
    console.log(creator2_ids)
})()

Upvotes: 0

Views: 70

Answers (2)

iftekhar
iftekhar

Reputation: 447

Index is string so use quote. see below

let the_ids = index === '0'? creator1_ids : creator2_ids

instead of

let the_ids = index === 0  ? creator1_ids: creator2_ids

Upvotes: 2

David
David

Reputation: 219087

The conclusion is invalid because the debugging is flawed. Look more closely in your debugger, specifically on this line:

let the_ids =
  index === 0 ?
  creator1_ids :
  creator2_ids

index never equals 0. It does equal '0'. So the === will never be true.

Change the comparison and you'll get the result you are expecting:

let the_ids =
  index == 0 ? // compare values regardless of type
  creator1_ids :
  creator2_ids

Then your loop will first modify creator1_ids, then creator2_ids. Demo:

async function runQuery(input) {
  return input
}

async function createAccounts() {
  return [1, 2]
}



(async function() {
  var creator1_ids = {},
    creator2_ids = {}

  let all_accounts = await createAccounts(2)

  const tables = [
    "list",
    "display_list",
    "map"
  ]

  for (let index in all_accounts) {
    let req = {
      session: {
        account: null
      }
    }
    req.session.account = all_accounts[index]
    let the_ids =
      index == 0 ?
      creator1_ids :
      creator2_ids

    // Create item
    let result = await runQuery()

    the_ids.item = 1

    for (let table of tables) {
      result = await runQuery(table)
      the_ids[table] = result
    }
  }

  console.log(creator1_ids)
  console.log(creator2_ids)
})()

Upvotes: 3

Related Questions