Search code examples
javascriptarrayspositionrow

Problems locating 'rows' within JavaScript arrays


I am finding JS arrays to be maddening, I am attempting to locate the 'row' of a passed item in a (JS) 2D array. I would not be having these issues in other languages. I have attempted two completely different approaches, neither of which is providing a proper result. This is how I am creating the 2D array, and it seems to be functional:

var _rooms = [];

var _User = function(roomNo, name) {
  this.roomNumber = roomNo;
  this.LeaderName = name;
};

_rooms.push( new _User(1, "katy") );
_rooms.push( new _User(23, "Sara") );

Here is the first attempt to locate a row # of a passed 'name':

function findPosition(str) {
var _locater, _sought, _seeks;  
  
 for (_locater = 0; _locater < _rooms.length; _locater++) {
 _seeks = _rooms[_locater];
 _sought = _seeks.indexOf(str);     
   if (_sought >= 0) {
     return "row: " + _locater + ", col: " + _sought;
   }
 }
}

console.log(findPosition('Sara'));
//SOURCE:  "https://stackoverflow.com/questions/46540878/finding-row-and-column-of-a-multidimensional-array-in-javascript"

This throws a typeError, something like "_seeks.indexOf(str) is not a function". Here is another attempt:

function indexOf2dArray(itemtofind) {
var _sought
  
_sought = [].concat.apply([], ([].concat.apply([], _rooms))).indexOf(itemtofind);
            
// return "false" if the item is not found
if (_sought === -1) { return false; }

// Use any row to get the rows' array length
// Note, this assumes the rows are arrays of the same length
numColumns = _rooms[0].length;

// row = the index in the 1d array divided by the row length (number of columns)
row = parseInt(_sought / numColumns);

// col = index modulus the number of columns
col = _sought % numColumns;

return [row, col]; 
}

console.log("Sara is located: " + indexOf2dArray("Sara"))  
console.log("katy is located: " + indexOf2dArray("katy")) 
//SOURCE:  https://lage.us/Javascript-Item-in-2d-Array-Using-indexOf.html

The result of this approach is "false" for each of the console.log statements. Can anybody suggest a reliable method to locate the 'row' a searched item appears in a JavaScript 2D array...? Any suggestions much appreciated.


Solution

  • Keeping most of your original code you can do something like this:

    var _rooms = [];
    
    var _User = function (roomNo, name) {
      this.roomNumber = roomNo;
      this.LeaderName = name;
    };
    
    _rooms.push(new _User(1, 'katy'));
    _rooms.push(new _User(23, 'Sara'));
    
    function findPosition(str) {
      var _locater, _sought, _seeks;
    
      for (_locater = 0; _locater < _rooms.length; _locater++) {
        _seeks = _rooms[_locater]; // _seeks is a _User
        _sought = _seeks.LeaderName.indexOf(str);
    
        if (_sought >= 0) {
          return 'row: ' + _locater + ', col: ' + _seeks.roomNumber;
        }
      }
    }
    
    console.log(findPosition('Sara')); // prints "row: 1, col: 23"
    console.log(findPosition('kat'));  // prints "row: 0, col: 1"
    console.log(findPosition('Joe'));  // prints "undefined"
    

    Another way could be using more "modern" JavaScript:

    function findPosition2(str) {
      const roomIndex = _rooms.findIndex((user) => user.LeaderName.includes(str));
    
      if (roomIndex >= 0) {
        return `row: ${roomIndex}, col: ${_rooms[roomIndex].roomNumber}`;
      } else {
        return 'not found';
      }
    }
    
    console.log(findPosition2('Sara')); // prints "row: 1, col: 23"
    console.log(findPosition2('kat'));  // prints "row: 0, col: 1"
    console.log(findPosition2('Joe'));  // prints "not found"