首页 \ 问答 \ 列表列表的Array.indexOf不起作用(Array.indexOf for list of lists is not working)

列表列表的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
更新时间:2022-05-15 08:05

最满意答案

问题是在全局范围内查找分配函数,不使用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); 这只有一个模板 ...
  • template void 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 ...
  • 代码中有两个不同的问题,第一个是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 ...
  • 我发现了问题。 实际上很傻,但导致问题的类层次结构有一个名为GetObject的虚函数,它与具有相同名称的windows #define冲突。 头文件以不同的顺序包含这些Windows头文件,这使链接器混乱。 所以,事实上问题是vtable已损坏,但我没想到这是理由! 那么你每天都学到一些东西...... 但是,非常感谢所有回复! I found the problem. Silly really but the class hierarchy that caused the problem had a v ...
  • 作为一般经验法则,应在标题中定义模板。 如果在cpp文件中定义它们,则需要手动实例化模板。 您正在为类模板执行此operator* ,但您没有实例化operator*模板。 template TVector3 operator* (T, const TVector3 &); 另外,我建议不要使用模板化operator* ,而是通过在类模板中声明和定义它来使用非模板自由函数: template class TVector3 { //function needed ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)