我發現 JS 陣列令人抓狂,我試圖在 (JS) 2D 陣列中找到傳遞專案的“行”。我不會在其他語言中遇到這些問題。我嘗試了兩種完全不同的方法,但都沒有提供正確的結果。這就是我創建二維陣列的方式,它似乎很實用:
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];
_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"
這會引發 typeError,類似于“_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 陳述句,這種方法的結果都是“錯誤的”。任何人都可以建議一種可靠的方法來定位搜索專案出現在 JavaScript 2D 陣列中的“行”...?任何建議都非常感謝。
uj5u.com熱心網友回復:
如果您熟悉我認為具有可定義類的其他語言,為什么不使用JavaScript 類。
創建一個User類,以及一個Users具有陣列的類,您可以將每個用戶物件添加到其中。
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'));
附加檔案
findIndex模板/字串文字
解構賦值
uj5u.com熱心網友回復:
保留大部分原始代碼,您可以執行以下操作:
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"
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/463663.html
標籤:javascript 数组 位置 排
