[英]What is the point of creating a function in JavaScript Object that then creates property?
[英]Javascript point to an object property
我有一個像這樣的數組:
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));
}
}
}
例如, element
代表意見。 當我做console.log(eval(elements[index]. element));
它未正確返回p31/php/endpoint.php?get=opinions
但返回未定義。 我還嘗試使用eval()
來評估從字符串到可運行代碼的element
,但是什么也沒有。
我做錯了什么? 謝謝
采用
elements[index][element]
但這不僅僅是您想要的嗎?
elements[element]
另外,使用===代替== ...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.