1

How can I search in a two dimensional array and return the array list as a array?

var dataSet = new Array(new Array());
function searchAsset(){
    dataSet.length = 0;
    var fileName = document.getElementById("fileName").value;
    var arr = new Array(["view_page.psd","test"],["ok_test.jpg","yes","kk"],["new_banner_2009.fla","good"],["gps-new-web.html","hot"]);
    var re = new RegExp(fileName ,'i');
    var res = null;
    for(var i = 0;i<arr.length;i++){
            var newArr = arr[i][0];
            //alert(newArr+":"+newArr.match(re));
            var res = newArr.match(re);
        if(res != null){
            dataSet.push("["+arr[i]+"]");
        }
    }
    alert("Final --- "+dataSet);
    for(var m = 0;m<dataSet.length;m++){
        alert(dataSet[m]);
    }
}
1
  • Don't paste HTML, just the code, and use code formatting Commented Feb 19, 2010 at 6:51

1 Answer 1

2

This:

var dataSet = new Array(new Array());

...is not a two-dimentional array. It results in an array with (initially) one element, which is a reference to another array with (initially) zero elements. The closest you'll come to a two-dimensional array in JavaScript is an array of arrays, such as:

function create2DArray(rows, columns) {
    var outer, index;

    outer = new Array(rows);
    for (index = 0; index < rows; ++index) {
        outer[index] = new Array(columns);
    }

    return outer;
}

Searching through such a thing is easy:

function find(thing, theArray) {
    var results, col, row, subArray;

    results = []; // Empty array
    for (row = 0; row < theArray.length; ++row) {
        subArray = theArray[row];
        for (col = 0; col < subArray.length; ++col) {
            value = subArray[col];
            if (value == thing) { // or whatever your criterion
                results.push({row: row, col: col});
            }
        }
    }

    return results;
}

That creates an array of objects, each with an col and row property, that tells you where the things in theArray are such that theArray[row][col] == thing.

Note that rather than doing theArray[row][col] every time in the loop, I grab the sub-array reference once for each row. Also note that since theoretically the sub-arrays could have different lengths, I don't assume they all have the same length.

I recommend reading up on JavaScript's arrays, which are fairly different from traditional arrays. They're really dictionaries, associative arrays, not contiguous blocks of memory indexed by an offset. When you do theArray[5], you're looking up a property called "5" on the array in exactly the way that when you say theObject['frog'] you're looking up a property "frog" on the object. (Not that implementations can't optimize that, but since JavaScript arrays are sparse, they have to be careful when doing so.)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.