I have Array in the name of students in that array I have four different objects. Now I have to print those objects, but the age should be in ascending order and I have to do this only by using forEach
only.
var students = [{ name: 'Mark', age: 28 }, { name: 'Johnson', age: 30 }, { name: 'Williams', age: 12 }, { name: 'Henry', age: 27 } ] students.forEach(fun) function fun(currentValue, index, arr) { console.log(currentValue) }
You don't use forEach
to sort an array, you use sort
(unsurprisingly!)
var students = [{ name: 'Mark', age: 28 }, { name: 'Johnson', age: 30 }, { name: 'Williams', age: 12 }, { name: 'Henry', age: 27 } ] students.sort(sortByAge).forEach(fun) function sortByAge(a,b){ return a.age - b.age; } function fun(currentValue, index, arr) { console.log(currentValue) }
I think you have to sort the arrya and print it.
Hope this works.
var students = [{ name: 'Mark', age: 28 }, { name: 'Johnson', age: 30 }, { name: 'Williams', age: 12 }, { name: 'Henry', age: 27 } ] function sortArray( a, b ) { if ( a.age < b.age ){ return -1; } if ( a.age > b.age ){ return 1; } return 0; } students.sort( sortArray ); students.forEach(fun) function fun(currentValue, index, arr) { console.log(currentValue) }
I hope this is a dynamic solution which is used your system in future as well.
function dynamicSort(property) {
var sortOrder = 1;
if(property[0] === "-") {
sortOrder = -1;
property = property.substr(1);
}
return function (a,b) {
/* next line works with strings and numbers,
* and you may want to customize it to your needs
*/
var result = (a[property] < b[property]) ? -1 : (a[property] > b[property]) ? 1 : 0;
return result * sortOrder;
}
}
and it will work when you do:
students.sort(dynamicSort("name"));
students.sort(dynamicSort("name"));
If I'm understanding the problem correctly, you might be allowed to do a simple bubble sort.
Assuming you had something like this
var students = [{
name: 'Mark',
age: 28
},
{
name: 'Johnson',
age: 30
},
{
name: 'Williams',
age: 12
},
{
name: 'Henry',
age: 27
}
]
console.log(bubbleSort(students.map(x => x.age)))
You might be able to use one of these two bubble sort algorithms
let bubbleSort = (inputArr) => {
let len = inputArr.length;
inputArr.forEach(function(i){
for (let j = 0; j < len; j++) {
if (inputArr[j] > inputArr[j + 1]) {
let tmp = inputArr[j];
inputArr[j] = inputArr[j + 1];
inputArr[j + 1] = tmp;
}
}
})
return inputArr;
};
let bubbleSort = (inputArr) => {
let len = inputArr.length;
let swapped;
do {
swapped = false;
inputArr.forEach(function(number, i){
if (inputArr[i] > inputArr[i + 1]) {
let tmp = inputArr[i];
inputArr[i] = inputArr[i + 1];
inputArr[i + 1] = tmp;
swapped = true;
}
})
} while (swapped);
return inputArr;
};
To sort any data it's requred minimum O(nlogn) operations, but forEach func will called only n times. You hence unable to do it using forEach only.
But if you really want to use forEach only, let's use some magic with async js:
var students = [{
name: 'Mark',
age: 28
},
{
name: 'Johnson',
age: 30
},
{
name: 'Williams',
age: 12
},
{
name: 'Henry',
age: 27
}
]
students.forEach(fun)
function fun(currentValue) {
setTimeout(() => console.log(currentValue), currentValue.age);
}
Please, don't use this solution in production
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.