This question already has answers here:
Javascript Equivalent to C# LINQ Select
(8 answers)
Closed 6 years ago.
I am new to Javascript and I was wondering is there a similar function in Javascript like C# Select(). My task is from array of people to sort the age of them and select only age of each person and print it. And this is what i come up with:
ageArraySorted = args.sort(function(person1, person2) {
return person1.age - person2.age;
});
I sorted them and now I need only the values of age property to be printed.
Without a library like linq.js the closest analog is the map method on Array;
ageArraySorted = args.sort(function(person1, person2) {
return person1.age - person2.age;
}).map(function(item) {
return item.age;
});
be careful with Map as a new to javascript
map does not mutate the array on which it is called (although callback, if invoked, may do so).
var numbers = [1, 4, 9];
var roots = numbers.map(Math.sqrt);
// roots is now [1, 2, 3]
// numbers is still [1, 4, 9]
and map was added to the ECMA-262 standard in the 5th edition;
Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support (Yes) 1.5 (1.8) 9 (Yes) (Yes)
from ...https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/map
Related
This question already has answers here:
Bubble sort algorithm JavaScript [closed]
(7 answers)
Closed 2 years ago.
im new in this forum so, if i wrong to post this i apologize. Im learnin Javascript and for training i made this bubblesort algorithm:
var x = [1, 5, 2, 8, 3, 6, 4, 9, 7];
sort(x);
function sort(params) {
var appoggio=0;
for (i=0; i<params.length; i++) {
for (j=0; j<params.length; j++) {
appoggio=params[j];
params[j] = params[j+1];
params[j+1] = appoggio;
}
}
}
console.log(x);
I have made a basic html page where i call this script but it doesn't work and i don't understand why. I try to debugg it inser some alert(params[j]) inside the for cycle but after the first interation the scrpt blocks all the web page. What i have to do?
You are falling in an infinite loop as you push at j+1 even when you hit the end of your list (which increment the length each time). Try to stop at params.length-1.
The if condition is missing!
You need to swap values only if they are not ordered correctly.
Plus, as #MetallimaX said, you need to stop your loop at params.length - 1 to avoid writing outside of the array
Without giving you the answer, which would spoil everything:
for a given number of times (enough that values can bubble all the way) {
for each adjacent values pair (from left to right) {
if the pair is not ordered correctly {
swap both elements()
}
}
}
This question already has answers here:
Does JavaScript have a method like "range()" to generate a range within the supplied bounds?
(88 answers)
Closed 3 years ago.
can I please ask how to make a range in Javascript? For example if I need to print letters "A" to "E" or number 1 to 5. For example in Ruby it is simple double dot like this (1..5).
I tried this code but it gives error.
let letter = range("A", "E");
console.log(letter);
Thank You
For numbers you can use ES6 Array.from(), which works in everything these days except IE:
Shorter version:
Array.from({length: 20}, (x,i) => i);
Longer version:
Array.from(new Array(20), (x,i) => i)
which creates an array from 0 to 19 inclusive. This can be further shortened to one of these forms:
Array.from(Array(20).keys())
// or
[...Array(20).keys()]
Lower and upper bounds can be specified too, for example:
Array.from(new Array(20), (x,i) => i + *lowerBound*)
An article describing this in more detail: http://www.2ality.com/2014/05/es6-array-methods.html
Javascript doesn't include that feature, but you can use Lodash which is javascript library which include that feature, and It's only for number.
But you can create your self function which generate range of number and for letters
This question already has answers here:
Tersest way to create an array of integers from 1..20 in JavaScript
(16 answers)
Closed 6 years ago.
The community reviewed whether to reopen this question 2 months ago and left it closed:
Original close reason(s) were not resolved
Say I have a number 18, instead of an array, in hand.
What is the best way to create a functional loop in JS given a number X instead of array of X elements?
I can do this:
[1,2,3].forEach(function(){
));
but if I have the number 3
I can do
for(var i = 0; i < 3; i++){
}
but I want that loop to be functional instead
If you have a number and you want to create a loop then you can use the number in limiter condition in the for loop.
for(var i = 0; i < number; i++)
Edit 1: you can use foreach on arrays only, in that case since you have a number already you can create a array of that length and then use the foreach on it.
var foo = new Array(number).fill(0);
foo.foreach()
Also another option is
var N = 18;
Array.apply(null, {length: N}).map(Number.call, Number)
result [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17]
Many more options available in this thread Create a JavaScript array containing 1...N
I don't understand why you want to do this. An equivalent to:
[1,2,3].forEach(function(){ ... ));
Is
var limit = n;
while (--limit) {( // Note: 0 is falsy
function(){ ... }
)(limit);}
Or if you really want to use an array structure, the following will do:
new Array(limit).fill(0).forEach(function(){...});
You might be interested in Myth of the Day: Functional Programmers Don't Use Loops.
Per this question, you can "functionally" iterate over a linear sequence relatively easily using:
Array.apply(null, Array(number)).map(function () {}).forEach(...)
Not sure what advantage this gives you versus a regular for-loop with an index, though it is a neat trick.
I'm learning about the map() method right now and I understand very basic examples.
var numbers = [2, 4, 6];
var double = numbers.map(function(value) {
return value * 2;
});
My question is, in what cases do developers use the map() method to help solve problems? Are there some good resources with real world examples?
Thanks for the help!
As #Tushar referred:
The map() method creates a new array with the results of calling a
provided function on every element in this array.
So it is basically used when you need to apply certain functionality to every single element of an array and get the result back as an array with the new results.
For example doubling the numbers:
var numbers = [1, 4, 9];
var doubles = numbers.map(function(num) {
return num * 2;
});
// doubles is now [2, 8, 18]. numbers is still [1, 4, 9]
It basically helps to shorten your code eliminating the need of using for loop. But do remember it is used when every element of the array is manipulated because map() generates similar length of array provided.
For eg.- in the example you provided doubles will have [2, 8, 18].
where 2 correspond to 1.
4 correspond to 8.
9 correspond to 18.
I recommend you to watch the whole video but your answer is at the 14th minute:
Asynchronous JavaScript at Netflix by Matthew Podwysowski at JSConf Budapest 2015
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
JavaScript: min & max Array values?
Generate max 'N' values from javascript array
var arr = new Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
How to execute 5 max values from this array?
result must be like
10, 9, 8, 7, 6
Check out the Arrays Java API doc. It'll give you some practice in reading up on documentation as well as give you methods to use to solve the homework problem ;)
you can try this
1. first you arrange Array in ascending order.
2. get it's Length.
3. get last 5 Digits whose u have required.
i thinks this is helpful to you.
..! cheers