简体   繁体   English

您可以在Javascript中创建对对象数组的引用吗?

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

I'm new to Javascript and coding in general, so please forgive me if this question is silly. 我是Java语言和编码技术的新手,所以如果这个问题很傻,请原谅我。 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. 当前,完全相同的代码块使用了8-10次,并且该代码块需要定期更新,这意味着我们必须将同一代码块更新8-10次。 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. 内部的更改仅限于添加新的投标人(bidder6,bidder7等带有自己的参数),这意味着数组中的对象数量可能会增加。 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). 另外请注意,脚本上有大约20多个广告单元,其中许多具有相同的对象数组(标书及其参数),但是某些广告单元的数组略有不同(参数的值不同)。 There is a total of 4-5 different sets of arrays I need to be able to reuse in the script. 为了能够在脚本中重用,总共需要4-5个不同的数组集。 So I need to be able to refer to these separately. 因此,我需要能够分别参考这些内容。

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. 这意味着原始数组将改变。

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. 与一些合适的clone实现。

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 并将getBids函数更改为

const getBids = (names) => names .map (name => allBids [name] )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM