admin管理员组文章数量:1278365
I want to ask a question that I have about a 2D array in JavaScript, similar to this:
var array = [[2,45],[3,56],[5,67],[8,98],[6,89],[9,89],[5,67]]
That is, on every index, I have an array as well.
Suppose that I have a value in my second index like 56
, and I want the corresponding 1st index (i.e. 3 in above example).
I can do it using a loop (if no other option), but is there any better way?
Also, I know indexOf
method in JavaScript, but it's not returning the index when I call it like this:
array.indexOf(56);
Any insight will be helpful.
I want to ask a question that I have about a 2D array in JavaScript, similar to this:
var array = [[2,45],[3,56],[5,67],[8,98],[6,89],[9,89],[5,67]]
That is, on every index, I have an array as well.
Suppose that I have a value in my second index like 56
, and I want the corresponding 1st index (i.e. 3 in above example).
I can do it using a loop (if no other option), but is there any better way?
Also, I know indexOf
method in JavaScript, but it's not returning the index when I call it like this:
array.indexOf(56);
Any insight will be helpful.
Share Improve this question edited Jul 2, 2016 at 23:15 Warren Sergent 2,5974 gold badges37 silver badges44 bronze badges asked Jun 18, 2012 at 10:09 A_userA_user 2,1576 gold badges26 silver badges33 bronze badges5 Answers
Reset to default 14Use some iterator function.
filter
allows you to iterate over an array and returns only the values when the function returns true
.
Modern browsers way:
var arr = array.filter( function( el ) {
return !!~el.indexOf( 56 );
} );
console.log( arr ); // [3, 56]
console.log( arr[ 0 ] ); // "3"
Legacy browsers way, using jQuery:
var arr = $.filter( array, function() {
return !!~$.inArray( 56, $( this ) );
} );
console.log( arr ); // [3, 56]
console.log( arr[ 0 ] ); // "3"
You could do like this:
var ret;
var index = array.map(function(el){return el[1];}).indexOf(56);
if (index !== -1) {
ret = array[index][0];
}
array.indexOf( x )
returns the position of x
in the array, or -1 if it is not found. In your 2D array this is useless, since 56 isn't in array; it is in an array which is contained in array
.
You will have to loop over the first array, then use indexOf
to check each sub-array.
I found that in IE6 at least (I'm not sure about the more recent versions), the built-in Array type didn't have an indexOf method. If you're testing in IE, perhaps that's why you couldn't get started. I wrote this little addition to include in all my code:
if(!Array.prototype.indexOf){
Array.prototype.indexOf = function(obj){
for(var i=0; i<this.length; i++){
if(this[i]===obj){
return i;
}
}
return -1;
};
}
I can't really take credit for this. It's probably a fairly generic piece of code I found in some tutorial or one of Doug Crockford's articles. I'm quite sure someone will come along with a better way of writing it.
I find working in Firefox is a good place to start because you can install the Firebug console and get a better idea where things are going wrong. I admit that doesn't solve cross-browser problems, but at least it allows you to get a start on your project.
This little piece of code will ensure you can use the indexOf method to find where a given value is in a one-dimensional array. With your 2-dimensional array, each element in the outer array is an array not a single value. I'm going to do a little test to see exactly how that works and get back to you, unless someone else comes in with a better answer.
I hope this at least gets you started.
UPDATE Assuming you have a browser that implements Array.indexOf, I've tried every which way to pass one of the array elements in your outer array.
alert(array.indexOf([3,56]))
alert(array.indexOf("3,56"))
just return -1.
Oddly,
alert(array.indexOf(array[1]))
correctly returns 1. But you need to know the index of the inner array to get the index! I can't see a way of passing an array to the indexOf method.
Please try the below code, it is working
var array = [[2, 45], [3, 56], [5, 67], [8, 98], [6, 89], [9, 89], [5, 67]];
array.map((x, index) => {
if (x.includes(56)) {
console.log(index);
}
})
本文标签: javascriptUsing indexOf method to find value in a 2D arrayStack Overflow
版权声明:本文标题:javascript - Using indexOf method to find value in a 2D array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738400534a2084753.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论