简体   繁体   English

如何找到一个数组的多个索引值来调用另一个数组,javascript

[英]how to find multiple index values of an array to call on another array, javascript

Here's what I've got so far.这是我到目前为止所得到的。 I have two arrays, I've found the conditional values of a numeric array 'grade'(grades >= 60), I'm now trying to find the index values that correspond with those array values that are >= 60, so that I may then call on these index(es) on another array 'names' to return the students 'names' who correspond to the passing grade.我有两个数组,我找到了数值数组“等级”的条件值(等级 >= 60),我现在试图找到与那些 >= 60 的数组值对应的索引值,以便然后我可以在另一个数组“姓名”上调用这些索引以返回与及格成绩相对应的学生“姓名”。

Any pointers or tips greatly appreciated, thank you.非常感谢任何指针或提示,谢谢。

let total = 0;
let highest = 0;
let stuAvg= 0;  
let arr = [];


var names= [];
var grade= [];
var pos1 = [];



for (var i = 0; i < 10; i++) {             
    names.push(prompt('Enter The Ten Student Names | Student: ' + (i + 1))); 
    document.getElementById("stuNames").innerHTML=("Student's Names:" +names.join(','));
}
for (var i = 0; i < 10; i++) {              
    grade.push(Number(prompt('Enter The Ten Student Grades | Grade: ' + (i + 1))));
    total +=grade[i]; 
    stuAvg = total / grade.length;
    highest = Math.max(...grade);


    document.getElementById("stuGrades").innerHTML=("Student's Grades:" +grade.join(','));
}
function passing(grade) {
    return grade >= 60; 
}

function results() {
    pos1 = grade.indexOf(highest);
    arr = grade.filter(passing);


    document.getElementById("result").innerHTML=("Highest Grade:"+highest+"The Highest Grade 
          Belongs to:"+names[pos1]+" The Average Grade is "+stuAvg+"");
    document.getElementById("passing").innerHTML=("The Students who are passing are 
          "+arr+","+names+"");     
}

After this line:在这一行之后:

total +=grade[i];

If it's a passing grade, add the index of that student to a passingGradeIndexes array:如果是及格成绩,则将该学生的索引添加到passingGradeIndexes 数组中:

if (passing(grade[i])) passingGradeIndexes.append(i);

Then output the names:然后输出名称:

for (var i = 0; i < passingGradeIndexes.Length; i++) {
document.Write( names[passingGradeIndexes[i]] );
}

You can store those indexes in another array when doing the filter.执行过滤器时,您可以将这些索引存储在另一个数组中。

 function results() { pos1 = grade.indexOf(highest); var passingStudents = []; // to store passing indexes arr = grade.filter((grade, index) => { var pass = grade >= 60; if(pass){ passingStudents.push(index); } return pass; }); passingStudents = passingStudents.map(index => names[index]); // change indexes for names document.getElementById("result") .innerHTML=("Highest Grade:"+highest +"\\n The Highest Grade Belongs to:"+names[pos1] +"\\nThe Average Grade is "+stuAvg+""); document.getElementById("passing") .innerHTML=("The Students who are passing are: " + passingStudents.join(", ")); }

Now you have the indexes you need in another array.现在您在另一个数组中拥有了所需的索引。

It says you're a new contributor.它说你是一个新的贡献者。 Welcome to the community.欢迎来到社区。 Here are some tips on how to improve your code.这里有一些关于如何改进代码的提示。

1. Don't abbreviate 1. 不要缩写

While some accepted technical terms and established practices are abbreviated, such as HTML , CSS , and XHR , don't abbreviate words like students in your code.虽然一些公认的技术术语和既定做法是缩写的,例如HTMLCSSXHR ,但不要在代码中缩写students词。 The practice of abbreviating comes from a time when computers had limited memory and screens were small, where you could only type something like maybe a hundred characters per line or something.缩写的做法来自于计算机内存有限且屏幕很小的时代,在那里你只能输入每行大约一百个字符之类的东西。

2. Use functions and name them well 2.使用函数并命名好

I think what you're trying to do is get an input of students' names and grades, and get some statics on them, correct?我认为您要做的是输入学生的姓名和成绩,并对其进行一些静态分析,对吗? If so, you can write your code as the following:如果是这样,您可以将代码编写如下:

function getStudentNames() { /* write your code here */ }
function getStudentGrades() { /* write your code here */ }
function analyse(students, grades) { /* write your code here */ }
function renderResults(analysis) { /* write your code here */ }

// and then run the whole thing here:
function main() {
  var students = getStudentNames()
  var grades = getStudentGrades()
  var analysis = analyze(students, grade)
  renderResults(analysis)
}

With this structure in place we just have to worry about filling out the portion that says /* write your code here */ for each of the function.有了这个结构,我们只需要担心为每个函数填写/* write your code here */的部分。 This is just one example of how you might want to structure your code.这只是您可能希望如何构建代码的一个示例。 As you will later learn in your developer journey, this makes it easier to test later on.正如您稍后将在您的开发者之旅中了解到的那样,这使得以后的测试更容易。

Generally speaking, when you name a function, you want to name it in the following format: verbNoun() .一般来说,当你命名一个函数时,你希望以如下格式命名: verbNoun() For example, you might see functions like fetchData() , renderImage() , getShape() , etc. Sometimes you see functions that don't follow this, such as then() , or render() .例如,您可能会看到fetchData()renderImage()getShape()等函数。有时您会看到不遵循此规则的函数,例如then()render() In any case, don't use a noun as your function name.在任何情况下,都不要使用名词作为函数名。

3. Learn (more) array methods. 3. 学习(更多)数组方法。

While using for-loop has some advantages, for most context, the performance boost is negligible and using array methods provides clarity and readability.虽然使用 for 循环有一些优势,但对于大多数情况,性能提升可以忽略不计,而使用数组方法提供了清晰性和可读性。

For instance, summing up an array of number can be achieved as follows:例如,总结一个数字数组可以实现如下:

var sum = [0,1,2,3,4].reduce(function(current, sum) {
  return current + sum
}, 0)

// more modern
const sum = [0,1,2,3,4].reduce((current, sum) => current + sum, 0)

console.log(sum) // prints 10

Helpful ones can be found at Mozilla Developers Network , which is generally an excellent source of learning anything web-related.可以在Mozilla Developers Network找到有用的内容,这通常是学习任何与 Web 相关的内容的极好来源。

-- ——

I hope this helps我希望这有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM