Can you create references to an array of objects in Javascript?

156 views Asked by At

I'm new to Javascript and coding in general, so please forgive me if this question is silly. I was wondering if it is possible to reuse or refer to a block of code, specifically an array of objects. Currently, the exact same block of code is used 8-10 times, and the block of code needs to be updated regularly, meaning we have to update the same block of code 8-10 times. If there was a way to define the block of code so that it can be referred to/reused, and changes to this one block of code would be mirrored throughout the references, then the script would not only become much easier to manage, but it will also become half its original size.

The script is used for ad delivery through prebid and header bidding, if that matters. Basically the setup is an array containing Ad Units, and each Ad Unit contains an identifier code, allowed size formats, and an array of bidders. Its this array of bidders I want to reuse. I will provide an example of the current code for one ad unit (anonymized) below.

I've not been successful in finding any solutions anywhere online so far.

var adUnits = [
    {
        code: "0000001",
        sizes: [[980, 300], [980, 150]],
// The code below is what I want to reuse
        bids: [
        {
            bidder: "bidder1",
                params: { 
                    mid: 000001,
                    adxDomain: 'adx.domainhere.net'
                }
        },{
            bidder: "bidder2",
                params: { 
                    accountId: '00002',
                    siteId: '00002',
                    zoneId: '000002'
                }
        },{
            bidder: "bidder3",
                params: { 
                    mid: 000003,
                    adxDomain: 'adx.domeinhere.net'
                }
        },{
            bidder: 'bidder4',
                params: {
                    placementId: '00000004'
                }
        },{
            bidder: 'bidder5',
                params: {
                    placementId: '00000005',
                    keywords: {
                        'no-sno-publishergroup': ['nameIdentifier']
                              },
                    user: {
                        externalUid: getAdId()
                          }
                }
    }]

Can anyone point me in the right direction?

Edit:

The changes inside are limited to adding new bidders (bidder6, bidder7 etc. with their own parameters), meaning the number of objects in the array may increase. Possibly, the order of the bidders could also change, so a solution that does not rely on indexing would be great.

Another note, we have around 20+ ad units on a script, many of these have identical arrays of objects (bidders and their parameters), but some ad units have slightly different arrays (the values of the parameters are different). There is a total of 4-5 different sets of arrays I need to be able to reuse in the script. So I need to be able to refer to these separately.

2

There are 2 answers

0
Scott Sauyet On

It's not entirely clear to me what you want. Below is my best guess. We centralize the list of bids and a function to retrieve a list of them by name. Then the main code can just call that function wherever it's needed.

const getAdId = ((n) => () => n++)(0) // dummy

//  `allBids` and `getBids` need to be in some central location

const allBids = {
    bidder1: {
       mid: '000001',
       adxDomain: 'adx.domainhere.net'
    },
    bidder2: {
       accountId: '00002',
       siteId: '00002',
       zoneId: '000002'
    },
    bidder3: {
        mid: '000003',
        adxDomain: 'adx.domeinhere.net'
    },
    bidder4: {
       placementId: '00000004'
    },
    bidder5: {
       placementId: '00000005',
       keywords: {
          'no-sno-publishergroup': ['nameIdentifier']
       },
       user: {
          externalUid: getAdId()
       }
   }
   // all other bids here.
}

const getBids = (names) => names .map (name => ({
    bidder: name,
    params: allBids [name] || {}
}))


// This needs to have a reference to `getBids`, via an `import`, `require` or whatever mechanism you choose.

var adUnits = [{
    code: "0000001",
    sizes: [[980, 300], [980, 150]],
    // Now you can just pick the bids you want from the list
    bids: getBids(["bidder1", "bidder2", "bidder3", "bidder4", "bidder5"])
}]

console .log (
  adUnits
)

// somewhere else
// ... getBids(['bidder3', 'bidder7', 'bidder2', 'bidder6']) 

Here the list of parameters is shared by reference among any callers, but the overall bidder object is not. Either would be easy to change. To stop sharing by reference, you might do

const getBids = (names) => names .map (name => ({
    bidder: name,
    params: clone(allBids [name] || {})
}))

with some suitable clone implementation.

If you want to share the whole object, just store it inside the main object:

const allBids = {
    bidder1: {
        bidder: 'bidder1`,
        params: {
           mid: '000001',
           adxDomain: 'adx.domainhere.net'
        }
    }
    /* ... */
}

and change the getBids function to

const getBids = (names) => names .map (name => allBids [name] )
0
Rahul Rana On

You can the same array. As wherer you use them as passing in argument or modify directly, it happens by reference. It means the original array will change.