makutene
makutene

Reputation: 29

how can I show array of array¿

I just want a way to return my tile map (array of an array)in the textarea, I mean at position [0][1] for example it paints a # for 1 values and empty fill for 0 ones.

<script language="javascript">
var wall = "#";
var empty = "";
var mapa = [
    [1, 1, 1, 1, 1],
    [1, 0, 0, 0, 1],
    [1, 0, 0, 0, 1],
    [1, 1, 1, 1, 1]
];

function map() {
    for (i = 0; i < mapa.length; i++) {
        for (j = 0; j < mapa[i].length; j++) {
            mapa[i][j];
        }
    }
}

function display(form) {
    var myform = form;
    myform.caja.value = map();
}
</script>
<body>
<form name=form>
<textarea name=caja cols=5 rows=5></textarea><br>
<input type=button value="click!" onclick="display(this.form)">
</form>

Upvotes: 0

Views: 49

Answers (2)

Bergi
Bergi

Reputation: 664971

I'd go with

function print2dim(arr, charmap) {
    return arr.map(function(row) {
        return row.map(function(el) { return charmap[el]; }).join('');
    }).join("\n");
}

function display(form) {
    form.caja.value = print2dim(mapa, [" ", "#"]); // or {"0":" ","1":"#"}
}

(demo at jsfiddle.net)

Upvotes: 0

TheVillageIdiot
TheVillageIdiot

Reputation: 40517

try this:

function map(){
    var ret='';
    for(i=0;i<mapa.length;i++){
        for(j=0;j<mapa[i].length;j++){
            if(mapa[i][j]===0){
                ret +='#';
            }else{
                ret +=' ';
            }
        }
        ret +='\n';
    }
    return ret;
}

Upvotes: 1

Related Questions