Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

indexOf() 2d array value

Posted on 2016-10-20
2
Medium Priority
?
87 Views
Last Modified: 2016-10-26
How can I get the array index of a value in a 2d array?
Here's my jsfidle
https://jsfiddle.net/isogunro/2h3wf4q4/
0
Comment
Question by:Isaac
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 22

Accepted Solution

by:
Kim Walker earned 1400 total points
ID: 41853116
Your multi-dimensional array definition is generating an error. You cannot define ary[0][0] until you have established that ary[0] is an array. This is true for ary[1] through ary[4] also. Once you have properly defined your multi-dimensional array, you would have to search each tier of the array to find the index of "Boat."
var ary = [];

ary[0] = ["Car","Boat"];
ary[1] = ["Car","Boat"];
ary[2] = ["Car","Boat"];
ary[3] = ["Car","Boat"];
ary[4] = ["Car","Boat"];

for (var i in ary) {
  console.log(ary[i].indexOf("Boat"));
}

Open in new window

0
 
LVL 59

Assisted Solution

by:Julian Hansen
Julian Hansen earned 600 total points
ID: 41853414
To expand on Kim's solution.
You can declare the array upfront like so
var ary = [[],[],[],[],[]];

Open in new window


Output changed a bit to show array and index
var ary = [[],[],[],[],[]];

ary[0][0] = "Car";
ary[0][1] = "Boat";
ary[1][0] = "Car";
ary[1][1] = "Boat";
ary[2][0] = "Car";
ary[2][1] = "Boat";
ary[3][0] = "Car";
ary[3][1] = "Boat";
ary[4][0] = "Car";
ary[4][1] = "Boat";

for(var i in ary){
  var aryIndex = ary[i].indexOf("Boat");
  // Added array and index in array
  console.log('Array[' +i + '] index: ' + aryIndex);
 }

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question