javascript - Two-dimensional string array creation -


i'm trying create two-dimensional array of 1-character strings in javascript way:

var panel = new array(3).fill(new array(3).fill('*')); 

result is

[     ['*', '*', '*'],     ['*', '*', '*'],     ['*', '*', '*'] ] 

after need replace middle string:

panel[1][1] = '#'; 

and result is

[     ['*', '#', '*'],     ['*', '#', '*'],     ['*', '#', '*'] ] 

so seems prototype fill function fills array object reference.

is possible force fill function fill array different object instances? if no, approach of creation this-like two-dimensional array in opinion best?


update

algorithm assumes create plane of asterisks, , lot of asterisks replaced symbols creating reasonable result. after printed.

in c done using matrix of chars , changing symbols @ indices. far cannot change single symbol in javascript string, i'm trying find analogue.

currently i've created two-dimensional array using 2 loops, changing symbols need, , after map , join functions i'm joining result single string. best solution?

thank you.

not sure efficiency of method converting array string using json.stingify() , parsing original format json.parse() makes objects loose original reference , work without pain.

var panel = new array(3).fill(new array(3).fill('*'));  var copy = json.parse(json.stringify(panel));    copy[1][1] = '#';    //you can assign value of variable 'copy' 'panel' if required.    document.write(json.stringify(copy)); //for demo

you can simplify above logic single liner of code without variable, way

var panel = json.parse(json.stringify(new array(3).fill(new array(3).fill('*'))));  panel[1][1] = '#';    document.write(json.stringify(panel)); //for demo


Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -