代码之家  ›  专栏  ›  技术社区  ›  brooksrelyt Ozal Zarbaliyev

你能从函数中选择数组中的一个特定位置吗?

  •  0
  • brooksrelyt Ozal Zarbaliyev  · 技术社区  · 6 年前

    不知道该问什么。但是,我的代码如下。我想做的是使用listPlanets();在if语句中返回用户在提示符中键入的数组项。

    任何帮助都很好。

    var planets = [
        {planet: 'Mercury', position: '1', orbit_time: '0.24', nat_satellites: '0'},
        {planet: 'Venus', position: '2', orbit_time: '0.62', nat_satellites: '0'},
        {planet: 'Earth', position: '3', orbit_time: '1', nat_satellites: '1'},
        {planet: 'Mars', position: '4', orbit_time: '1.88', nat_satellites: '2'},
        {planet: 'Jupiter', position: '5', orbit_time: '11.86', nat_satellites: '67'},
        {planet: 'Saturn', position: '6', orbit_time: '29.46', nat_satellites: '62'},
        {planet: 'Uranus', position: '7', orbit_time: '84.32', nat_satellites: '27'},
        {planet: 'Neptune', position: '8', orbit_time: '164.79', nat_satellites: '14'}
    ];
    
    var listPlanets = function () {
        for (var i = 0; i < planets.length; i++) {
            document.write(planets[i].planet + ' is planet #' + planets[i].position + 
                ' from the Sun. Time to complete its orbit is ' + planets[i].orbit_time + ' earth year(s). It has ' + planets[i].nat_satellites + ' natural satellite(s).<br>' );
        }
    }
    
    // listPlanets();
    
    var num = window.prompt("Please enter a number between 1 and 8");
    
    if (1 <= num && num <= 8) {
        listPlanets();
    } else {
        alert("The value you entered is not within range. Please reload the page and enter a value thatis within 1 and 8, inclusive."); 
        window.location.reload();
    }
    
    3 回复  |  直到 6 年前
        1
  •  2
  •   brettinternet mxstbr    6 年前

    @dgeare的答案之所以有效,是因为数组索引与position属性值匹配。您还可以通过添加一个简单的 if 你的声明 listPlanets

    var planets = [
        {planet: 'Mercury', position: '1', orbit_time: '0.24', nat_satellites: '0'},
        {planet: 'Venus', position: '2', orbit_time: '0.62', nat_satellites: '0'},
        {planet: 'Earth', position: '3', orbit_time: '1', nat_satellites: '1'},
        {planet: 'Mars', position: '4', orbit_time: '1.88', nat_satellites: '2'},
        {planet: 'Jupiter', position: '5', orbit_time: '11.86', nat_satellites: '67'},
        {planet: 'Saturn', position: '6', orbit_time: '29.46', nat_satellites: '62'},
        {planet: 'Uranus', position: '7', orbit_time: '84.32', nat_satellites: '27'},
        {planet: 'Neptune', position: '8', orbit_time: '164.79', nat_satellites: '14'}
    ];
    
    var listPlanets = function (userInput) {
       for (var i = 0; i < planets.length; i++) {
           if (planets[i].position == userInput) {
              document.write(planets[i].planet + ' is planet #' + planets[i].position + 
                  ' from the Sun. Time to complete its orbit is ' + planets[i].orbit_time + ' earth year(s). It has ' + planets[i].nat_satellites + ' natural satellite(s).<br>' ); 
           }
       }
    }
    
    // listPlanets();
    
    var num = window.prompt("Please enter a number between 1 and 8");
    
    if (1 <= num && num <= 8) {
        listPlanets(num);
    } else {
        alert("The value you entered is not within range. Please reload the page and enter a value thatis within 1 and 8, inclusive."); 
        window.location.reload();
    }
        2
  •  1
  •   dgeare    6 年前

    我想您的意思是将输入的值作为参数传递到listPlanets函数中?

    var planets = [
        {planet: 'Mercury', position: '1', orbit_time: '0.24', nat_satellites: '0'},
        {planet: 'Venus', position: '2', orbit_time: '0.62', nat_satellites: '0'},
        {planet: 'Earth', position: '3', orbit_time: '1', nat_satellites: '1'},
        {planet: 'Mars', position: '4', orbit_time: '1.88', nat_satellites: '2'},
        {planet: 'Jupiter', position: '5', orbit_time: '11.86', nat_satellites: '67'},
        {planet: 'Saturn', position: '6', orbit_time: '29.46', nat_satellites: '62'},
        {planet: 'Uranus', position: '7', orbit_time: '84.32', nat_satellites: '27'},
        {planet: 'Neptune', position: '8', orbit_time: '164.79', nat_satellites: '14'}
    ];
    
    var listPlanets = function (i) { //i is a parameter that will be passed into the function at the time it is called
       i -= 1;//the user entered a value between 1 and 8, but our array indexes are from 0 - 7. decrease input by one
       document.write(planets[i].planet + ' is planet #' + planets[i].position + 
                ' from the Sun. Time to complete its orbit is ' + planets[i].orbit_time + ' earth year(s). It has ' + planets[i].nat_satellites + ' natural satellite(s).<br>' );
        
    }
    
    // listPlanets();
    
    var num = window.prompt("Please enter a number between 1 and 8");
    
    if (1 <= num && num <= 8) {
        listPlanets(num); //pass num into listPlanets function as argument
    } else {
        alert("The value you entered is not within range. Please reload the page and enter a value thatis within 1 and 8, inclusive."); 
        window.location.reload();
    }

    正如在一些注释中指出的,您可能更喜欢将数据反映到DOM,而不是document.write,这将覆盖现有页面,通常被认为是一种不好的做法。如果改为在DOM中执行此操作,它可能会如下所示:

    var planets = [
        {planet: 'Mercury', position: '1', orbit_time: '0.24', nat_satellites: '0'},
        {planet: 'Venus', position: '2', orbit_time: '0.62', nat_satellites: '0'},
        {planet: 'Earth', position: '3', orbit_time: '1', nat_satellites: '1'},
        {planet: 'Mars', position: '4', orbit_time: '1.88', nat_satellites: '2'},
        {planet: 'Jupiter', position: '5', orbit_time: '11.86', nat_satellites: '67'},
        {planet: 'Saturn', position: '6', orbit_time: '29.46', nat_satellites: '62'},
        {planet: 'Uranus', position: '7', orbit_time: '84.32', nat_satellites: '27'},
        {planet: 'Neptune', position: '8', orbit_time: '164.79', nat_satellites: '14'}
    ];
    
    var listPlanets = function (i) {
       i -= 1;
       document.getElementById('planet_name').innerHTML = planets[i].planet;
       document.getElementById('planet_info').innerHTML = planets[i].planet + ' is planet #' + planets[i].position + 
                ' from the Sun. Time to complete its orbit is ' + planets[i].orbit_time + ' earth year(s). It has ' + planets[i].nat_satellites + ' natural satellite(s).<br>';
        
    }
    
    // listPlanets();
    
    
    document.getElementById('info_button').addEventListener('click', function(evt){
      var num = document.getElementById('planet_input').value;
      if (1 <= num && num <= 8) {
          listPlanets(num);
      } else {
          alert("The value you entered is not within range. Please reload the page and enter a value thatis within 1 and 8, inclusive."); 
    
      }
    });
    Pick a planet between 1 and 8 <input id='planet_input' type='number' /><button id='info_button'>View Info</button>
    <h1 id='planet_name'></h1>
    <h3 id='planet_info'></h3>
        3
  •  0
  •   Ivo Zhulev    6 年前

    应将在提示中输入的索引作为函数参数传递,并使用它获取数组中的给定项:

    var planets = [...];
    
    var listPlanets = function (num) {
        var planet = planets[num];
    
        var thingToWrite = planet.planet + ' is planet #' + planet.position + ' from the Sun. Time to complete its orbit is ' + planet.orbit_time ' +  earth year(s). It has ' + planet.nat_satellites + ' natural satellite(s).<br>'
    
        document.write(thingToWrite);      
    }
    
    var num = window.prompt("Please enter a number between 1 and 8");
    
    if (1 <= num && num <= 8) {
        listPlanets(num);
    } else {
        alert("The value you entered is not within range. Please reload the page and enter a value thatis within 1 and 8, inclusive."); 
        window.location.reload();
    }
    

    伊沃