列表列表的Array.indexOf不起作用(Array.indexOf for list of lists is not working)
我有一个列表来自
ajax response
的服务器的列表具有以下结构:var mapping = [ //response.mapping ["userOne", 1], ["userTwo", 2], ["userthree", 3], ["userfour", 4], ["personOne", 1], ["personTwo", 2], ["personOne", 3], ];
由于名单将永远保持不变,这将只有7个元素在任何时候。 我想索引它的任何元素来更新一些DOM元素。
// Gives me any of list element which is random elm of the list `mapping`. var captured_list_elm = process_mapping_list(); var status = mapping.indexOf(captured_list_elm);// getting always -1
在这里,我总是得到
-1
状态。
- 可能是什么原因?
- 为什么indexOf无法计算其索引?
- 找到它的方法是什么,只通过循环遍历列表来获取索引?
创建了一个jsFiddle
注 - 事实上它应该是一个json,但是我们团队中的某个人已经把它写成了一个列表清单。 我现在无法更新它,因为代码正在生产中。
I have a list of lists which comes from the server in
ajax response
has following structure:var mapping = [ //response.mapping ["userOne", 1], ["userTwo", 2], ["userthree", 3], ["userfour", 4], ["personOne", 1], ["personTwo", 2], ["personOne", 3], ];
As the list will be constant forever, which will have only 7 elements at anytime. I want index of any of its element to update some DOM elements.
// Gives me any of list element which is random elm of the list `mapping`. var captured_list_elm = process_mapping_list(); var status = mapping.indexOf(captured_list_elm);// getting always -1
Here I'm getting always
-1
for status.
- What could be the reason?
- Why indexOf can not calculate its index?
- What's the way to find it, get index through looping over list only?
Created a jsFiddle
NOTE - Actually it should be a json but someone out of our team had written it as a list of lists. I can not update it as of now because the code is in production.
原文:https://stackoverflow.com/questions/30520401
最满意答案
问题是在全局范围内查找分配函数,不使用ADL查找。 由于在类中定义的友元函数对于封闭范围是隐藏的,因此找不到该函数。
5.3.4 / 9:
如果new-expression以unary :: operator开头,则在全局范围中查找分配函数的名称。 否则,如果分配的类型是类类型T或其数组,则在T的范围内查找分配函数的名称。如果此查找未能找到名称,或者如果分配的类型不是类类型,则分配函数的名称在全局范围内查找。
The problem is that the allocation function is looked up in the global scope, it is not looked up using ADL. Since friend functions defined inside a class are hidden from the enclosing scope, the function is not found.
5.3.4/9:
If the new-expression begins with a unary :: operator, the allocation function’s name is looked up in the global scope. Otherwise, if the allocated type is a class type T or array thereof, the allocation function’s name is looked up in the scope of T. If this lookup fails to find the name, or if the allocated type is not a class type, the allocation function’s name is looked up in the global scope.
相关问答
更多-
问题是在全局范围内查找分配函数,不使用ADL查找。 由于在类中定义的友元函数对于封闭范围是隐藏的,因此找不到该函数。 5.3.4 / 9: 如果new-expression以unary :: operator开头,则在全局范围中查找分配函数的名称。 否则,如果分配的类型是类类型T或其数组,则在T的范围内查找分配函数的名称。如果此查找未能找到名称,或者如果分配的类型不是类类型,则分配函数的名称在全局范围内查找。 The problem is that the allocation function is lo ...
-
你的实物不匹配。 您的初始声明和后来的定义具有以下签名: template
Matrix operator*(const Matrix &m1, const T &m2); 这是一个带有两个模板参数的函数模板: T和N 但是,在您的班级中,您将具有此签名的功能模板作为朋友: template friend Matrix operator* (const Matrix &m1, const T &m2); 这只有一个模板 ... -
C ++模板类,模板成员友元函数匹配规则(C++ template class, template member friend function matching rules)[2022-08-14]
templatevoid foo(T2 const &) template auto foo(T2 const &) -> std::enable_if_t ::value>; 是2个不同的重载。 即使两者都返回void (有效时)。 第二次过载使用SFINAE。 (是的,模板函数只能通过与常规函数相反的返回类型而不同)。 您的版本不完全相同但是类似( &std::remove_reference_t ::ope ... -
请尝试使用此函数签名(未测试): template
Vector operator*(const F &lhs, const Vector & rhs); 它应该允许以下语句: auto vec = f * other_vec; 对于具有该类型的已定义运算符的任何类型F : template undetermined operator*(const F& lhs, const T &rhs); ... -
对我来说,它看起来像VS2005使用Baz的第一个模板规范 template
struct Baz {}; 这个结构体确实不包含名为func的成员。 看起来像VS2005不会正确推导模板参数。 For me it looks like VS2005 uses the first template specification of Baz template struct Baz {}; This struct does indeed not contai ... -
C ++:friend declaration'声明一个非模板函数(C++ : friend declaration ‘declares a non-template function)[2021-09-23]
代码中有两个不同的问题,第一个是friend声明(正如警告中明确指出的,也许不那么清楚)理解为一个非模板函数作为朋友。 也就是说,当你实例化模板NVector它将一个非模板化函数std::ostream& operator<<(std::ostream&,NVector )为朋友。 请注意,这与声明您作为朋友提供的模板函数不同。 我建议你在类定义中定义friend函数。 您可以在此答案中阅读更多相关内容。 template ... -
确保dll的pdb文件也在应用程序的路径中。 或者使用/ Z7编译器选项编译您的dll,这将触发在MSVC 6.0中使用的旧式调试信息。 如果你这样编译,你将只能复制DLL。 或者在“模块”窗口中,右键单击您的dll并选择“加载符号”,然后将对话框指向您的dll的原始位置。 Make sure the pdb file for the dll is also in the application's path. Or alternatively compile your dll with the /Z7 c ...
-
关键问题是您声明的朋友与您之前提供的声明不同。 第一个需要两个模板参数,但是您定义的第二个(朋友)只接受一个。 一旦解决了,一切正常: template
class C; template void func(C & t); template class C { template friend void func(C& t); private: tem ... -
VS2005 C ++打破了vtables(VS2005 C++ broken vtables)[2023-05-01]
我发现了问题。 实际上很傻,但导致问题的类层次结构有一个名为GetObject的虚函数,它与具有相同名称的windows #define冲突。 头文件以不同的顺序包含这些Windows头文件,这使链接器混乱。 所以,事实上问题是vtable已损坏,但我没想到这是理由! 那么你每天都学到一些东西...... 但是,非常感谢所有回复! I found the problem. Silly really but the class hierarchy that caused the problem had a v ... -
模板类中的朋友功能(Friend function in template class)[2022-06-08]
作为一般经验法则,应在标题中定义模板。 如果在cpp文件中定义它们,则需要手动实例化模板。 您正在为类模板执行此operator* ,但您没有实例化operator*模板。 template TVector3operator* (T, const TVector3 &); 另外,我建议不要使用模板化operator* ,而是通过在类模板中声明和定义它来使用非模板自由函数: template class TVector3 { //function needed ...