检查ID是否存在,然后重命名 [英] Check if ID exists, and then rename it

查看:166
本文介绍了检查ID是否存在,然后重命名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



http://jsfiddle.net/HB7ev/10/



如果我理解正确:

  if(id in dupeUIDCheck)

检查是否 id 的值在数组中: dupeUIDCheck ,但是,似乎:

  dupeUIDCheck [id] = true; 

最终设置实际上使支票工作。



它并没有真正看到:

  dupeUIDCheck = $(document).find('DIV ')
.map(function(){return this.id || undefined})
.toArray();

为我打电话的价值。



如何解决这个问题?由于某种原因,我确实在我自己的网站的一部分工作,所以 makeIdUnique()似乎是应该这样工作,但为什么不这样例? (可能是一个格式不正确的数组?)



原来的函数是从以前的问题中分派的:重命名Javascript数组中的重复项

解决方案

@TrySpace



据了解,dupeUIDCheck是一个常规数组。如果这是真的,那么你需要使用Array.prototype.indexOf方法:

  if(dupeUIDCheck.indexOf(id)! = -1){
...
}

这段代码dupeUIDCheck [id] = true;解决了这个问题,因为你在数组对象本身上定义了属性,in操作符显然会确认这样的属性存在,但是对于数组元素来说这不是真的。 in操作符不在数组元素内查找。


I'm trying to detect duplicate IDs,

http://jsfiddle.net/HB7ev/10/

And if I understand correctly:

if (id in dupeUIDCheck) 

Checks if the value of id is in the array: dupeUIDCheck, however, it seems that the:

dupeUIDCheck[id] = true;

that is set at the end actually makes the check work.

It doesn't really look inside the:

dupeUIDCheck = $(document).find('DIV')
            .map(function(){ return this.id || undefined})
            .toArray();

for values, that I call first.

How can I fix this? For some reason I do have it working in one part of my own website, so the makeIdUnique() seems to be working like it should, but why doesn't it in this example? (maybe a malformed array?)

The original function was forked from a previous problem: Renaming duplicates in Javascript Array

解决方案

@TrySpace

As I understand "dupeUIDCheck" is a regular array. If this is true, then you need to use Array.prototype.indexOf method:

if (dupeUIDCheck.indexOf(id) != -1) {
 ...
}

This piece of code "dupeUIDCheck[id] = true;" fixes the problem because you are defining property on array object itself and "in" operator will obviously confirm that such property exists, though, this is not true for array elements. "in" operator doesn't lookup inside array elements.

这篇关于检查ID是否存在,然后重命名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆