动态传递父对象以用作javascript

时间:2018-10-27 14:46:23

标签: javascript object javascript-objects

这是我在论坛中的第一个问题。在问你们之前,我已经搜索了很多东西,但是也许是因为我仍在培养我的JavaScript技能,所以我无法弄清楚。

我试图根据如下所示的URL动态地将对象作为参数传递。

 let createDataLayer = () => {

  //Creating objects with the values for each page
  someurl = {
    pageType: 'Content',
    institution: 'Institution',
    contentTopic: 'Membership',
    productCategory: '',
    productName: '',
  };

  //Attaching the right array to the actual url
  let actualURL = "/some-url/";
  actualURL = actualURL.replace(/-/g,"");
  actualURL = actualURL.replace(/\//g,"");

  //Function that applies the right content to the right page
  let applyingContent = (variable) => {
    console.log("Always come as string: ", typeof variable); //string
    console.log("Can't access the object: ", variable.pageType); //undefined
    console.log("If I call the variable itself, it's here: ", someurl); //the object logs ok
    window.dataLayerValues = [variable.pageType, variable.institution, variable.contentTopic, variable.productCategory, variable.productName];
    return window.dataLayerValues;
  }

  applyingContent(actualURL);

}
createDataLayer();

请问有人可以帮我吗?

我非常感谢!

3 个答案:

答案 0 :(得分:1)

通过其他变量中包含的字符串访问变量通常不是用javascript完成的。您的代码通常不必关心变量的名称。如果需要组织数据以便可以使用字符串键访问它,则应该使用一个对象,该对象的键与字符串相对应。看起来像这样:

const urls = {
    someurl: {
        pageType: 'Content',
        institution: 'Institution',
        contentTopic: 'Membership',
        productCategory: '',
        productName: '',
    }
}

然后使用urls[key]访问数据,其中key是您的字符串。

然后,只需对代码进行一些更改即可使用它:

let createDataLayer = () => {

    //Creating objects with the values for each page
    const urls = {
        someurl: {
            pageType: 'Content',
            institution: 'Institution',
            contentTopic: 'Membership',
            productCategory: '',
            productName: '',
            }
        }
  
    //Attaching the right array to the actual url
    let actualURL = "/some-url/";
    actualURL = actualURL.replace(/-/g,"");
    actualURL = actualURL.replace(/\//g,"");
  
    //Function that applies the right content to the right page
    let applyingContent = (variable) => {
      console.log("Strill a string: ", typeof variable); //string
      // object accessed with key: urls[variable]
      console.log("Can access the object: ", urls[variable].pageType); //undefined
      return  urls[variable];
    }
  
    applyingContent(actualURL);
  
  }
  createDataLayer();

这会将所有数据整齐地打包在一个对象中,而不是到处都有单独的变量。您可以传递该对象,对其进行更改等。

答案 1 :(得分:0)

似乎您正在将some-url作为值字符串“ / some-url /”分配到ActualURL中,然后将其传递给applyContent()。您需要将某人作为对象传递给applyContent()函数,以便使用其内部值。

答案 2 :(得分:0)

谢谢,Ele!你给我我需要的指示!所以我只是问谷歌“在变量名JavaScript转换字符串”。答案是我需要在将其传递给参数之前对字符串使用eval()函数。这是我需要做的最简单的方法:

let createDataLayer = () => {

  //Creating objects with the values for each page
  someurl = {
    pageType: 'Content',
    institution: 'Institution',
    contentTopic: 'Membership',
    productCategory: '',
    productName: '',
  };

  //Attaching the right array to the actual url
  let actualURL = "/some-url/";
  actualURL = actualURL.replace(/-/g,"");
  actualURL = actualURL.replace(/\//g,"");

  actualURL = eval(actualURL);//This is the line I needed =)

  //Function that applies the right content to the right page
  let applyingContent = (variable) => {
    console.log("Always come as string: ", variable.pageType); //string
    console.log("Can't access the object: ", variable.pageType); //undefined
    console.log("If I call the variable itself, it's here: ", someurl); //the object logs ok
    window.dataLayerValues = [variable.pageType, variable.institution, variable.contentTopic, variable.productCategory, variable.productName];
    return window.dataLayerValues;
  }

  applyingContent(actualURL);

}
createDataLayer();

非常感谢您以正确的方式指出我,@ Ele。谢谢大家!