Javascript 嵌套数组元素的索引

Javascript 嵌套数组元素的索引,javascript,arrays,indexof,Javascript,Arrays,Indexof,我需要创建一个旅程规划器:我有三条线路,有多个站点,所有线路都有一个共同点。这就是我到目前为止用javascript所做的;我不知道这是否有意义 我试图做的是…定义我旅程的起点和终点,尝试定义commonStop,这样我就可以接下来使用if/else语句来定义我旅程的一部分…但是我被卡住了。。。因为作为数组的数组,我不确定我使用的语法。有什么方法可以改进这段代码吗?让它更易读,更简单 如果你能解释一下你会怎么做的话,我会非常感激你的帮助,因为我已经是新手了,有点迷路了 line1 = ['Tim

我需要创建一个旅程规划器:我有三条线路,有多个站点,所有线路都有一个共同点。这就是我到目前为止用javascript所做的;我不知道这是否有意义

我试图做的是…定义我旅程的起点和终点,尝试定义commonStop,这样我就可以接下来使用if/else语句来定义我旅程的一部分…但是我被卡住了。。。因为作为数组的数组,我不确定我使用的语法。有什么方法可以改进这段代码吗?让它更易读,更简单

如果你能解释一下你会怎么做的话,我会非常感激你的帮助,因为我已经是新手了,有点迷路了

line1 = ['Times Square', '34th', '28th', '23rd', 'Union Square', '8th'];

line2 = ['6th', 'Union Square', '3rd', '1st'];

line3 = ['Grand Central', '33rd', 'Union Square' , 'Astor Place'];

var allLines = [line1, line2, line3];

console.log(allLines);

// Ask the user for the start point and start line; end point and endline.
// var startLine = prompt ('Which line is your start?');
// var startPoint = prompt('Where you are getting on at?');
// var endPoint = prompt ('Where do you wanna get of at?');
// var endLine = prompt('Which line is that?');

var startLine = 'line2';
var startPoint = '6th';
var endPoint = '1st';
var endLine = line2;

 // commonstop1 = allLines[0][4];
 // commonstop2 = allLines[1][1];
// commonstop3 = allLines[2][2];

//if my startline is equal to my end line, my journey can proceed
//else I need to stop.

  var ptJourney = function (startPoint, endPoint) {

    if (startLine === endLine) {
        console.log('Your journey goes from ' + startPoint + ' to ' + endPoint + ' without changing line!');
   }     

   else { 
          console.log('You need to change line at Union Square!');
   };      

 function findStart(array, startPoint) {

    for(var i = 0; i < allLines.length; i++) {

     for (var j=0; j < allLines.length[i]; j++){

        if(allLines[i][j].name === startPoint) {

           var startIndex = allLines.indexOf([i][j]);
           return(startIndex);
          }
         return -1;
      }
   }

 };

 function findEnd(array, endPoint) {

    for(var k = 0; k< allLines.length; k++) {

        for(var l = 0; l < allLines.length[k]; l++) {

            if(allLines[i][j].name === endPoint) {

               var endIndex = allLines.indexOf[k][l];

               return(endIndex) ;   
            }
            return -1;
          }
       }

      }
  }

   //Need to review
   var partOne = function(startPoint, stop) {

   var commonStopIndex = allLines.indexOf('UnionSquare');

   for (i = 0 ; i <= commonStopIndex ; i++) {

       var tripOne = [allLines.slice(startPoint, commonStopIndex)];
       console.log (tripOne);
    }

  };
line1=[‘时代广场’、‘第34’、‘第28’、‘第23’、‘联合广场’、‘第8’];
第2行=['6','Union Square','3','1'];
第3行=[‘中央大道’、‘第33大道’、‘联合广场’、‘阿斯特广场’];
var allLines=[line1,line2,line3];
控制台日志(所有行);
//向用户询问起始点和起始线;终点和终点线。
//var startine=prompt('哪一行是您的开始?');
//var startPoint=prompt('您在哪里上车?');
//var endPoint=prompt('你想从哪里得到?');
//var endLine=prompt('那是哪一行?');
var STARTINE='line2';
var起始点='6';
var端点='1st';
var endLine=line2;
//CommonTop1=所有行[0][4];
//CommonTop2=所有行[1][1];
//commonstop3=所有行[2][2];
//如果我的终点线等于终点线,我的旅程就可以继续了
//否则我需要停下来。
var pttrovely=函数(起点、终点){
如果(起始线===结束线){
log('您的旅程从'+startPoint+'到'+endPoint+'而不改变行!');
}     
否则{
log('您需要在联合广场换行!');
};      
函数findStart(数组、起始点){
对于(变量i=0;i对于(i=0;i这是一个可以做到的方法。我将其全部放入一个自闭包中,以便在加载代码时触发,但如果您想以另一种方式触发它,则可以将其放入一个命名函数中

(function(){
// Create an array of lines.
var lines = ['line1', 'line2', 'line3'];

// Create a two dimensional arrat of the station names.
var stations = [['Times Square', '34th', '28th', '23rd', 'Union Square', '8th'],
['6th', 'Union Square', '3rd', '1st'],
['Grand Central', '33rd', 'Union Square' , 'Astor Place']];

// Setup the other variables
var getStrLn, getStrPnt, getEndLn, getEndPnt, setStrLn, setStrPnt, setEndLn, setEndPnt;

// Create a series of window promts to collect and store user input.
getStrLn = prompt('What line are you starting at?');

// Get the index positions for the line so it can be used in the next prompt.
setStrLn = lines.indexOf(getStrLn);

getStrPnt = prompt('What station are you going to on line ' +lines[setStrLn]+ '?');
getEndLn = prompt('What line are you going to end at?');

// Get the index positions for the line so it can be used in the next prompt.
setEndLn = lines.indexOf(getEndLn);
getEndPnt = prompt('What station are you going to end at on line ' +lines[setEndLn]+ '?');


// Get the index positions of the station that is entered. 

setStrPnt = stations[setStrLn].indexOf(getStrPnt);

setEndPnt = stations[setEndLn].indexOf(getEndPnt);

// Alert the results.
alert('Your starting line is ' +lines[setStrLn]+ ' and your station is ' +stations[setStrLn][setStrPnt]+ '.\n Your ending line is ' +lines[setEndLn]+ ' and your ending station is ' +stations[setEndLn][setEndPnt]+ '.');
}());