gpt4 book ai didi

javascript - 如何检查对象的参数是否获取其他变量?

转载 作者:行者123 更新时间:2023-12-03 06:56:27 28 4
gpt4 key购买 nike

我必须检查浏览器的链接,如果它获取对象的参数,请显示什么参数获取此变量。这个怎么做?这是代码:

(function () {
var windowLink = "",
str = window.location.pathname.split("/"),
lastelement = str[str.length - 1],
pages = {
page_1 : "index.html", //Put the list of your pages here
page_2 : "index2.html",
page_3 : "index3.html",
page_4 : "index4.html",
page_5 : "index5.html"
};
for(lastelement in pages) {
windowLink = pages[lastelement];
console.log(windowLink)
}
})();

最佳答案

你几乎已经成功了,你只是覆盖了你的 lastelement 变量。

(function () {
var windowLink = "",
str = window.location.pathname.split("/"),
lastelement = str[str.length - 1],
pages = {
page_1 : "index.html", //Put the list of your pages here
page_2 : "index2.html",
page_3 : "index3.html",
page_4 : "index4.html",
page_5 : "index5.html",
page_6 : "js"
};

console.log("Looking for " + lastelement);

for(element in pages) {
windowLink = pages[element];

if (windowLink === lastelement) {
console.log("Found at " + element);
break;
}
}
})();

关于javascript - 如何检查对象的参数是否获取其他变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37257402/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com