Home > Mobile >  Problems locating 'rows' within JavaScript arrays
Problems locating 'rows' within JavaScript arrays

Time:04-25

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.

CodePudding user response:

If you're familiar with other languages which I imagine have definable classes why not use JavaScript classes.

Create a User class, and a Users class which has an array you can add each user object to.

Users can have a method that locates the index of the user whose name you pass into it.

class Users {

  // Add the array of users
  constructor(users) {
    this.users = users;
  }

  // Use `findIndex` to return the index
  // of the array given the `name` argument
  findPosition(name) {
    const index = this.users.findIndex(obj => {
      return obj.name === name;
    });
    return `${name} is on row ${index}`;
  }

};

class User {

  // Destructure the id and name from the user object
  constructor({ id, name }) {
    this.id = id;
    this.name = name;
  }
}

// `Users` accepts an array of user objects
const users = new Users([
  new User({ id: 1, name: 'Katy' }),
  new User({ id: 9, name: 'Boris' }),
  new User({ id: 23, name: 'Sara' })
]);

// And now you can call the users method
// to find the index of the user in the array
console.log(users.findPosition('Boris'));
console.log(users.findPosition('Sara'));
console.log(users.findPosition('Katy'));

Additional documentation

CodePudding user response:

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"
  • Related