简体   繁体   中英

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.

I have an array that consist of ages (4, 21, 47, 16, 18 etc). 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.

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). 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:

1)

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

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...

edit: So I have a plain web page with a textbox where you can insert age&name and save them. Then I have a possibility to show everyone who are not allowed to buy alcohol and those who are (under and over 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:

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.

and

I have also tried following, but these will give me only the first value under 18 (or not work in general).

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.

 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() .

 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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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