2015-03-08 40 views
-2

我想創建以下方法:如何通過函數的對象和屬性

// expects: (array of objects, name of property on object to compare 
       to value, value to compare to) 
    // returns: index of object in array 
    function findIndex(array, delegate, value) { 
    for (index in array) { 
     if (value === array[index].delegate) { 
     return index; 
     } 
    } 
    }; 

如果我通過在一個委託值是一個字符串以上方法不起作用。是這樣的可能嗎?

回答

2

您可以使用bracket notation,像這樣

if (value === array[index][delegate]) { 
    return index; 
} 
+0

哦verrrry爽!這次真是萬分感謝!我不知道那是存在的。像魅力一樣工作! – 2015-03-08 17:35:55