javascript - 变量中数组的长度
问题描述
我的程序根据用户输入的最后一个字母选择一个数组,并选择所选数组的名称。我想找到与用户输入的单词的最后一个字母同名的 arrey 的长度。字母/数组选择有效,但长度无效。基本是一字足球的风格。
我试过这个但它不起作用:
var a = ['quasar','Quebec','quercetin']
var b = ['boron','broky','branka']
var c = ['cop','camera','controler','copito']
//i dint write all of the variables becous it woud be to long but his works as an example.
function hrat() {
var input = document.getElementById("text").value;
var lastChar = input.substr(input.length - 1);
console.log (lastChar);
console.log ((lastChar).length);
var number = Math.floor(Math.random() * (lastChar).length);
解决方案
我只是了解您正在输入数组标识符,然后检查该数组的长度。如果我错了,请纠正我。
var a = ['quasar','Quebec','quercetin']
var b = ['boron','broky','branka']
var c = ['cop','camera','controler','copito']
//i dint write all of the variables becous it woud be to long but his works as an example.
function hrat() {
var input = document.getElementById("text").value;
var lastChar = input.substr(input.length - 1);
console.log (lastChar);
console.log (this[lastChar].length);
//OR
console.log (window[lastChar].length)
var number = Math.floor(Math.random() * this[lastChar].length);
推荐阅读
- c++ - 使用从源代码构建的 clang-11 编译 hello world 失败
- openshift - 如何在 Cloudflare Proxy 后面使用 Openshift?
- class - 如何覆盖 cakephp4 中 src 文件夹中的供应商插件类文件
- flutter - Flutter:'package:flutter/src/cupertino/route.dart':断言失败:第 660 行 pos 12:'_backGestureController != null':不正确
- javascript - 基于循环的扩展算法
- java - 解决 spark core 和 azure key vault 依赖项之间的番石榴冲突
- flutter - Flutter App开发中的VS Code调用堆栈行
- neo4j - 在查询结果中分离匹配节点
- python - 为什么不能直接等待 python 协程对象?
- oracle - Oracle CASE 语句优化