[英]Searching a 2D Javascript Array For Value Index
I am trying to write a jQuery that will find the index of a specific value within a 7x7 2D array. 我正在尝试编写一个jQuery,它将在7x7 2D数组中找到特定值的索引。
So if the value I am looking for is 0 then I need the function to search the 2D array and once it finds 0 it stores the index of the two indexes. 因此,如果我要查找的值为0,那么我需要该函数搜索2D数组,一旦找到0,它将存储两个索引的索引。
This is what I have so far, but it returns "0 0" (the initial values set to the variable. 到目前为止,这就是我所拥有的,但是它返回“ 0 0”(将初始值设置为变量。
Here is a jsFiddle and the function I have so far: 这是一个jsFiddle和我到目前为止的功能:
http://jsfiddle.net/31pj8ydz/1/ http://jsfiddle.net/31pj8ydz/1/
$(document).ready( function() {
var items = [[1,2,3,4,5,6,7],
[1,2,3,4,5,6,7],
[1,2,3,0,5,6,7],
[1,2,3,4,5,6,7],
[1,2,3,4,5,6,7],
[1,2,3,4,5,6,7],
[1,2,3,4,5,6,7]];
var row = 0;
var line = 0;
for (i = 0; i < 7; ++i) {
for (j = 0; i < 7; ++i) {
if (items[i, j] == '0,') {
row = i;
line = j;
}
}
}
$('.text').text(row + ' ' + line);
});
HTML: HTML:
<p class="text"></p>
You access your array with the wrong way. 您以错误的方式访问阵列。 Please just try this one: 请尝试以下一项:
items[i][j]
When we have a multidimensional array we access the an element of the array, using array[firstDimensionIndex][secondDimensionIndex]...[nthDimensionIndex]
. 当我们拥有多维数组时,可以使用array[firstDimensionIndex][secondDimensionIndex]...[nthDimensionIndex]
访问数组的元素。
That being said, you should change the condition in your if statement: 话虽如此,您应该在if语句中更改条件:
if( items[i][j] === 0 )
Please notice that I have removed the ,
you had after 0. It isn't needed. 请注意,我已经删除了,
你有0后不需要它。 Also I have removed the ''
. 我也删除了''
。 We don't need them also. 我们也不需要它们。
Your if
statement is comparing 您的if
语句正在比较
if (items[i, j] == '0,')
Accessing is wrong, you should use [i][j]
. 访问是错误的,您应该使用[i][j]
。
And your array has values: 并且您的数组具有值:
[1,2,3,4,5,6,7]
....
Your value '0,'
is a string, which will not match numeric values inside the array, meaning that your row and line won't change. 您的值'0,'
是一个字符串,将与数组中的数字值不匹配,这意味着您的行和行将不会更改。
First, you are accessing your array wrong. 首先,您访问数组错误。 To access a 2D array, you use the format items[i][j]
. 要访问2D数组,请使用items[i][j]
格式。
Second, your array doesn't contain the value '0'
. 其次,您的数组不包含值'0'
。 It doesn't contain any strings. 它不包含任何字符串。 So the row
and line
variables are never changed. 因此row
和line
变量永远不会改变。
You should change your if statement to look like this: 您应该将if语句更改为如下形式:
if(items[i][j] == 0) {
Notice it is searching for the number 0, not the string '0'. 注意,它在搜索数字0,而不是字符串“ 0”。
There are following problems in the code 代码中存在以下问题
1) items[i,j]
should be items[i][j]
. 1) items[i,j]
应该是items[i][j]
。
2) You are comparing it with '0,'
it should be 0
or '0'
, if you are not concerned about type. 2)您正在将其与'0,'
进行比较,如果您不关心类型,则应为0
或'0'
。
3) In your inner for
loop you should be incrementing j
and testing j
as exit condition. 3)在内部for
循环中,您应该递增j
并将j
作为退出条件进行测试。
Change your for
loop like bellow and it will work 像下面这样更改for
循环,它将起作用
for (i = 0; i < 7; i++) {
for (j = 0; j < 7; j++) {
if (items[i][j] == '0') {
row = i;
line = j;
}
}
}
Note:- 注意:-
1) Better to use ===
at the place of ==
, it checks for type also. 1)最好在==
处使用===
,它还会检查类型。 As you see with 0=='0'
gives true. 如您所见, 0=='0'
为true。
2) Better to say i < items.length
and j<items[i].length
instead of hard-coding it as 7. 2)最好说i < items.length
和j<items[i].length
而不是将其硬编码为7。
var foo;
items.forEach(function(arr, i) {
arr.forEach(function(val, j) {
if (!val) { //0 coerces to false
foo = [i, j];
}
}
}
Here foo will be the last instance of 0 in the 2D array. foo是2D数组中0的最后一个实例。
looks like you are still learning how to program. 看来您仍在学习编程。 But here is an algorithm I've made. 但是,这是我制定的算法。 Analyze it and compare to your code ;) 分析它并与您的代码进行比较;)
var itens = [[1,2,3,4,5,6,7],
[1,2,3,4,5,6,7],
[1,2,3,0,5,6,7],
[1,2,3,4,5,6,7],
[1,2,3,4,5,6,7],
[1,2,3,4,5,6,7],
[1,2,3,4,5,6,7]];
var row = null;
var collumn = null;
for (var i = 0; i < itens.length; i++) {
for (var j = 0; j < itens[i].length; j++) {
if (itens[i][j] == 0) {
row = i;
collumn = j;
}
}
}
console.log(row, collumn);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.