嵌套数组元素的索引

时间:2016-03-20 03:04:15

标签: javascript arrays indexof

我需要创建一个旅程规划器:我有三条线路有多个停靠点,所有这些线路都有一个共同点。这是我到目前为止用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);
    }

  };

1 个答案:

答案 0 :(得分:0)

这是一个做到这一点。我将它全部放入自闭合中,以便在加载代码时触发它,但是如果你用另一种方式触发它,你可以将它放入命名函数中。

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