admin管理员组文章数量:1430512
I have an array like this:
var arr = [];
arr['A string'] = '123';
arr['Another string'] = '456';
and Im trying to find an index of '123' or '456'.
Both:
var string = $.inArray('123', arr)
and
var string = arr.indexOf('123')
are giving me -1. Is it possible to get it working when indexes are strings?
I have an array like this:
var arr = [];
arr['A string'] = '123';
arr['Another string'] = '456';
and Im trying to find an index of '123' or '456'.
Both:
var string = $.inArray('123', arr)
and
var string = arr.indexOf('123')
are giving me -1. Is it possible to get it working when indexes are strings?
Share Improve this question edited Sep 9, 2013 at 12:51 hakre 199k55 gold badges450 silver badges856 bronze badges asked Nov 21, 2010 at 18:53 sNiCKYsNiCKY 1,8433 gold badges18 silver badges20 bronze badges 1- Your array is an object when using strings as indicies. – John Hartsock Commented Nov 21, 2010 at 18:56
2 Answers
Reset to default 3Like all Array methods and the length
property, the indexOf
method of an Array
object only considers numeric properties of the Array. These are properties whose names are unsigned integers. To test for the existence of a property with a particular value, you can do something like this on any object, including arrays. There's no guarantee about the enumeration order of property names, so if there's more than one property with a particular value then you can't be sure which property you'll get:
function findPropertyWithValue(obj, val) {
for (var i in obj) {
if (obj.hasOwnProperty(i) && obj[i] === val) {
return i;
}
}
return null;
}
var arr = [];
arr['A string'] = '123';
arr['Another string'] = '456';
alert(findPropertyWithValue(arr, "123")); // 'A string'
Note that since all property names, including numeric ones, are converted into strings, you can assign array properties using strings:
var arr = [];
arr["1"] = "foo";
arr[3] = "bar"
arr.indexOf("foo"); // 1
arr.indexOf("bar"); // 3
The problem is that you are using a JavaScript array as an associative array, something that it is not. The indices of a JavaScript array are unsigned 32 bit integers and therefore you can't use *strings**. You would either use an array like so
// I'm guessing that you meant to give numerical and not string values
var arr = [123, 456];
or use an object
var obj = {
'A string' : 123,
'Another string' : 456
};
Note that using an object, 'A string'
and 'Another string'
are properties of object obj
and can't be indexed like the values in an array. You can check that an object has a property a number of ways, one of which would be using hasOwnProperty
if (obj.hasOwnProperty('A string')) {
// if obj has property 'A string' as a direct property
}
another would be using the in
keyword
if ('A string' in obj) {
// if obj has a property 'A string' as a property (could be an inherited property too)
}
**unless the string is the string representation of a 32 bit unsigned integer as Tim points out, but I think it's fair to say that a lot of JavaScript developers would say stick to using integers for clarity.*
本文标签:
版权声明:本文标题:javascript - JS & jQuery: inArray() and indexOf() are not working properly if indexes are strings? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745446422a2658683.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论