accessing array within multidimensional array - javascript [duplicate] - javascript

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 3 years ago.
here is the problem
multiDimensionalArray = [1, 2, 3, [4, 5, 6, [7, 8, 9, [10, 11, 12, [13, 14, [15]]]]]];
I want to access number 15. I cant seem to figure it out.
p.s i am a newbee with js

multiDimensionalArray[3][3][3][3][2][0]
Each [n] is accessing an array value at index n.
So the above statement says:
1) take the 4th (index 3 is the 4th element due to indices starting at 0, not 1) element.
2) This happens to be an array. Take the 4th element again.
3) ...

Related

I wanna understand sort function working in javascript [duplicate]

This question already has answers here:
How to sort an array of integers correctly
(32 answers)
Sort number javascript array
(2 answers)
Closed 4 months ago.
If array A = [-1, -3, -2, 0];
if I try to sort it
like A.sort()
console.log(A)
return is [-1, -2, -3, 0] instead of [-3, -2, -1, 0]
The Array.prototype.sort accepts an optional callback function (a compare function) that you use to sort the array based on your preference.
In your example above, you didn't specify a compare function thus sort will default to:
sorting the elements of an array in place and returns the reference to the same array, now sorted. The default sort order is ascending, built upon converting the elements into strings, then comparing their sequences of UTF-16 code units values. Source: MDN
From the result sample you included, it seems you want to sort those numbers in an ascending order (smaller to bigger). To do so, you should pass a compare function to the sort method that describes that sorting logic you'd want to have.
Here's a live demo:
const myArray = [-1, -3, -2, 0];
/** Please check the linked docs page for more details */
console.log(myArray.sort((a, b) => a < b ? -1 : 1)); // or simply: "(a, b) => a - b)"
The above demo should print: [-3, -2, -1, 0].
You can do this:
A.sort((a, b) => a-b);

Sort function in JavaScript does not give correct output for some cases? [duplicate]

This question already has answers here:
How to sort an array of integers correctly
(32 answers)
Why can't JavaScript sort [5, 10, 1]? [duplicate]
(6 answers)
Closed 4 years ago.
I tried to use sort function to arrange the items in order to find the largest element in a number array.
In Chrome, inspect element and open the console.
>>var a = [1000, 1001, 857, 1]
>>undefined
>>a.sort()
>>(4) [1, 1000, 1001, 857] // no patern found.
Why did the numbers did not sort properly like they did for the other cases like these:
>>var b = [13, 27, 18, 26];
>>undefined
>>b.sort()
>>(4) [13, 18, 26, 27] // here the numbers are in ascending order.
That's because the default sort order treats the elements of the array as text, not numbers.
The default sort order is according to string Unicode code points.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

In what cases do javascript developers use the map() method?

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

Permutation crossover operator (genetic algorithm) problems when there isn't 1-for-1 mapping

This isn't homework, purely for my side project.
I'm implementing the Permutation Crossover Operator for genetic algorithm (solving travelling salesman, where each number represents a city index) and I'm having some problem with the boundary case when there isn't a 1-for-1 mapping.
Consider the two genomes below, and assuming the final two entries are switched. Thus, 5 is mapped to 6, and 6 is mapped to 7. So what happens when I hit a number 6 - should I change it to 5 or 7, and this can lead to an invalid tour where a city is visited twice.
//initial case
GenomeA: [ 2, 3, 1, 4, 0, 7, 5, 6 ]
GenomeB: [ 1, 2, 0, 3, 4, 5, 6, 7 ]
5 <--> 6
6 <--> 7
//after mapping
GenomeA: [ 2, 3, 1, 4, 0, 6, 6, 7 ]
GenomeB: [ 1, 2, 0, 3, 4, 6, 5, 6 ]
Should I randomly select another number if a number is already mapped? Or should I not switchover any numbers that already has been mapped?
For instance,
a) Evaluate first set of numbers (5 <--> 6)
b) Since 5 has not been mapped, map 5 to 6 and vice versa
c) Evaluate second set of numbers (6 <--> 7)
d) Since 6 is already mapped to 7, ignore this set of numbers
I found an easy way to produce legitimate offspring in the case of multiple mappings (5 <--> 6 <--> 7).
a) First check if any number outside the substring exchanged ("originalNumber" is contained within the mapping
b) Let the mapped value of "originaNumber" be called "replacement"
c) Check if "replacement" is also contained within the mapping (if it is, this implies that there will be a clash if we set "originalNumber" to "replacement") If it isn't, simply set "originalNumber" to "replacement"
d) Otherwise, find the mapped value of "replacement" and set "originalNumber" to it.

Automatically Generate max 'N' values from javascript array [duplicate]

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

Categories