在javascript中将父对象动态传递给函数作为参数
这是我在论坛上的第一个问题。在问你们之前,我已经搜索了很多,但也许因为我还在培养我的JavaScript技能,我想不出来 我试图根据如下所示的URL动态地将对象作为参数传递在javascript中将父对象动态传递给函数作为参数,javascript,object,javascript-objects,Javascript,Object,Javascript Objects,这是我在论坛上的第一个问题。在问你们之前,我已经搜索了很多,但也许因为我还在培养我的JavaScript技能,我想不出来 我试图根据如下所示的URL动态地将对象作为参数传递 let createDataLayer = () => { //Creating objects with the values for each page someurl = { pageType: 'Content', institution: 'Institution', co
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();
有人能帮我吗
我非常感激 似乎您正在将某个url作为值字符串“/some url/”分配给actualURL,然后将其传递给applyingContent()。您需要将某人作为对象传递给applyingContent()函数,以便使用其内部值。通过其他变量中的字符串访问变量不是javascript中通常执行的操作。您的代码通常不应该关心变量的名称。如果需要组织数据以便可以使用字符串键访问数据,则应使用具有与字符串对应的键的对象。看起来是这样的:
const urls = {
someurl: {
pageType: 'Content',
institution: 'Institution',
contentTopic: 'Membership',
productCategory: '',
productName: '',
}
}
然后使用url[key]
访问数据,其中key
是您的字符串
然后,只需对代码进行少量更改即可使用它:
让createDataLayer=()=>{
//使用每个页面的值创建对象
常量URL={
someurl:{
页面类型:“内容”,
机构:"机构",,
contentTopic:“会员资格”,
产品类别:“”,
产品名称:“”,
}
}
//将正确的数组附加到实际url
让actualURL=“/some url/”;
实际值=实际值。替换(/-/g,“”);
actualURL=actualURL.replace(//\//g,“”);
//将正确内容应用于正确页面的函数
让applyingContent=(变量)=>{
log(“Strill a string:,typeof变量);//string
//使用键访问的对象:URL[变量]
console.log(“可以访问对象:”,URL[variable].pageType);//未定义
返回URL[变量];
}
应用内容(实际量);
}
createDataLayer()代码>谢谢你,Ele!你给了我需要的方向!所以我只是问google“转换变量名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。谢谢大家! 但是,您正在传递actualrl
,它是一个字符串。你期待什么?嗨,艾莉,谢谢你的回答。如何获取该字符串并将其作为对象名传递,以便调用该对象,而不是applyingContent函数中的字符串?