Javascript指向一个对象属性
我有这样一个数组:Javascript指向一个对象属性,javascript,Javascript,我有这样一个数组: elements = new Array( {opinions: 'p31/php/endpoint.php?get=opinions'}, // function to call and API endpoint, php/endpoint.php {top3positive: 'p31/php/endpoint.php?get=top3positive'}, // function to call andAPI endpoint, php
elements = new Array(
{opinions: 'p31/php/endpoint.php?get=opinions'}, // function to call and API endpoint, php/endpoint.php
{top3positive: 'p31/php/endpoint.php?get=top3positive'}, // function to call andAPI endpoint, php/endpoint.php
{top3negative: 'p31/php/endpoint.php?get=top3negative'} // function to call andAPI endpoint, php/endpoint.php
);
我正在努力获得价值。举个简单的例子:elements[0]。opinions
将正确返回p31/php/endpoint.php?get=opinions
事实上,我需要在不直接知道值的情况下获得相同的信息
因此,我正在做以下工作:
function getEndPoint(element) {
for (index in elements) {
if (Object.getOwnPropertyNames(elements[index]) == element) {
console.log(eval(elements[index]. element));
}
}
}
元素
例如代表意见。
当我执行console.log(eval(elements[index].element))
它没有正确地返回p31/php/endpoint.php?get=options
,但返回未定义。
我还尝试使用eval()
将元素
从字符串计算为可运行代码,但什么也没有
我做错了什么?
谢谢使用
elements[index][element]
但这不只是你想要的吗
elements[element]
此外,如果可能,请使用===而不是==…避免使用
eval
。这不是一种设计良好的存储这些URL的方法。你能换一下吗?此外,在数组上的循环中,不应使用“for…”。。。使用一个简单的for
循环。很高兴我能帮上忙!非常感谢接受这个答案。