Click here to Skip to main content
15,885,890 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
var a1 = [[0,1],[0,1],[1,1]],
    a2 = [[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0]],
   res = a2.map((s,i) => s.map((n,j) => a1[i][j] !== void 0 ? a1[i][j] : n));

The code is supposed to overlay a1 onto a2, but how exactly does it do this?
What are the variables s, i, j, and n?
Code taken from answer to this question: How to impose a 2D array on top of another in JavaScript - Quora[^]

What I have tried:

I have tried putting the code in to jsfiddle where it finds some errors but it seems to work.
Posted
Updated 25-Feb-17 4:34am

 
Share this answer
 
Comments
Maciej Los 26-Feb-17 8:32am    
5ed!
To add to solution 1, this
res = a2.map((s,i) => s.map((n,j) => a1[i][j] !== void 0 ? a1[i][j] : n));
is the same as this:
res=a2.map(function(s,i){

	return s.map(function(n,j){

		if (a1[i][j] !== void 0){
			return a1[i][j];
		} else {
			return n;
		}
	
    })

})
in "normal" JavaScript. Learn the working of JavaScript Array map() Method[^]
 
Share this answer
 
Comments
Maciej Los 26-Feb-17 8:32am    
5ed!
Peter Leow 26-Feb-17 21:26pm    
Thank you, Maciej.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900