简体   繁体   English

如何根据年龄对数组进行排序

[英]How to sort array according to age

I have gone through the stackoverflow questions and have found answers that are similar/close to the issue I am having, but it seems my understanding is lacking. 我经历了stackoverflow问题,并找到了与我所遇到的问题相似/接近的答案,但是似乎我的理解不足。

I have an array that consist of ages (4, 21, 47, 16, 18 etc). 我有一个由年龄(4、21、47、16、18等)组成的数组。 I am trying to sort the array so that it would find every index position that is under 18. I have writen a for loop for this, but it terminates everytime there is a age over 18. 我正在尝试对数组进行排序,以便找到不超过18的每个索引位置。为此,我编写了一个for循环,但它会在年龄超过18岁时终止。

var text = "";

for (var i = 0; i <= myArray.length && myArray[i] < 18; i++) {
text += i;
}
console.log(text);
text = ""; // with this I get answer 0 to console, where as i would want
0 3.

I have also tried following, but these will give me only the first value under 18 (or not work in general). 我也尝试了以下方法,但是这些方法只会给我18以下的第一个值(或者通常不起作用)。 I have just started coding so I dont understand these ways as well so I would prefer to do it with for loop if possible: 我刚刚开始编码,所以我也不了解这些方式,因此,如果可能的话,我宁愿使用for循环进行编码:

1) 1)

function underEighteen() {
myArray.reduce(function a, e, i) {
if (e < 18)
a.push(i);
return a;
}, []);
}

2) 2)

function isUnderEighteen(elementBelow18) {
return elementBelow18;
}
function sortAge() {
var under18 = myArray.find(isUnderEighteen);
}

I have tried several other simple ways to sort the array to no avail. 我尝试了其他几种简单的方法来对数组进行排序都没有用。 Help appreciated! 帮助赞赏! Javascript only... 仅JavaScript ...

edit: So I have a plain web page with a textbox where you can insert age&name and save them. 编辑:所以我有一个带有文本框的纯网页,您可以在其中插入age&name并保存它们。 Then I have a possibility to show everyone who are not allowed to buy alcohol and those who are (under and over 18). 然后,我可以向所有人展示不允许购买酒类的人和那些(不满18岁的)人。

My aim is to do this so that I find the index positions of those elements that are under 18, pull the same elements from name array that correspond those of the age arrays on post them on the page with: 我的目的是这样做,以便找到18以下的那些元素的索引位置,从名称数组中拉出与age数组的元素相对应的相同元素,并在页面上使用以下元素:

document.getElementbyId("notAllowetoDrink").innerHTML = "Do not sell    
alcohol + "XXXXXXX;

Based on 基于

with this I get answer 0 to console, where as i would want 0 3. 有了这个我得到答案0控制台,因为我想要0 3。

and

I have also tried following, but these will give me only the first value under 18 (or not work in general). 我也尝试了以下方法,但是这些方法只会给我18以下的第一个值(或者通常不起作用)。

You were close , small issues are highlighted in comments 您很亲密 ,评论中突出了小问题

function underEighteen() {
   return myArray.reduce(function (a, e, i) { //return was missing and function syntax was incorrect
      if (e < 18)
      {
         a.push(i);
      }
      return a;
   }, []);
}

Demo 演示版

 var myArray = [4, 21, 47, 16, 18]; function underEighteen() { return myArray.reduce(function(a, e, i) { if (e < 18) { a.push(i); } return a; }, []); } console.log(underEighteen()); 

You could use your reduce approach and return the result. 您可以使用reduce方法并返回结果。

 function underEighteen(array) { return array.reduce(function(a, e, i) { if (e < 18) { a.push(i); } return a; }, []); } var ages = [4, 21, 47, 16, 18]; console.log(underEighteen(ages)); 

I think you need to use filter() instead of sort() . 我认为您需要使用filter()而不是sort()

 var arr = [4, 21, 47, 16, 18]; function under18(array){ return array.filter(function(x){return x < 18;}); } console.log(under18(arr)); 
 .as-console { height: 100%; } .as-console-wrapper { max-height: 100% !important; top: 0; } 

This will be the base code, you can either use the array directly or pass to the function as a local variable! 这将是基本代码,您可以直接使用数组,也可以将其作为局部变量传递给函数!

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

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