在 JavaScript 数组中定位 'rows' 时出现问题
Problems locating 'rows' within JavaScript arrays
我发现 JS 数组令人抓狂,我试图在 (JS) 二维数组中找到传递项目的 'row'。我不会在其他语言中遇到这些问题。我尝试了两种完全不同的方法,但都没有提供正确的结果。这就是我创建二维数组的方式,它似乎可以正常工作:
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") );
这是第一次尝试定位已通过的第 # 行 '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: "
这会抛出一个类型错误,类似于“_seeks.indexOf(str) 不是一个函数”。这是另一个尝试:
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
对于每个 console.log 语句,此方法的结果都是“假”。任何人都可以建议一种可靠的方法来定位 'row' 搜索到的项目出现在 JavaScript 二维数组中......?非常感谢任何建议。
如果您熟悉我认为可以定义的其他语言 class 为什么不使用 JavaScript classes.
创建一个 User
class 和一个 Users
class,其中有一个数组,您可以将每个用户 object 添加到其中。
Users
可以有一个方法来定位您将其名称传递给它的用户的索引。
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'));
其他文档
保留大部分原始代码,您可以这样做:
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"
另一种方法可能是使用更“现代”的 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"
我发现 JS 数组令人抓狂,我试图在 (JS) 二维数组中找到传递项目的 'row'。我不会在其他语言中遇到这些问题。我尝试了两种完全不同的方法,但都没有提供正确的结果。这就是我创建二维数组的方式,它似乎可以正常工作:
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") );
这是第一次尝试定位已通过的第 # 行 '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: "
这会抛出一个类型错误,类似于“_seeks.indexOf(str) 不是一个函数”。这是另一个尝试:
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
对于每个 console.log 语句,此方法的结果都是“假”。任何人都可以建议一种可靠的方法来定位 'row' 搜索到的项目出现在 JavaScript 二维数组中......?非常感谢任何建议。
如果您熟悉我认为可以定义的其他语言 class 为什么不使用 JavaScript classes.
创建一个 User
class 和一个 Users
class,其中有一个数组,您可以将每个用户 object 添加到其中。
Users
可以有一个方法来定位您将其名称传递给它的用户的索引。
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'));
其他文档
保留大部分原始代码,您可以这样做:
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"
另一种方法可能是使用更“现代”的 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"