嵌套数组元素的索引

Index of element of nested array

我需要创建一个行程规划器:我有 3 条线路,有多个停靠站,而且它们都有一个共同点。到目前为止,这就是我一直在做的 javascript;完全不知道有没有意义。

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

任何帮助将不胜感激..如果你能解释一下你会做什么,因为我真的很新,有点迷路了。

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);
    }

  };

这是一件一件的事情。我把它全部放在一个自闭包中,以便它在加载代码时触发,但如果你想以其他方式触发它,你可以将它放入一个命名函数中。

(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]+ '.');
}());