在控制台中获取数组值
getting array value in console
我有一个数组 (tot
),里面有数组。
我需要检查 (tot
) 中的每个条目的值,这是通过在控制台键入 AA[3] 完成的,但是,当我从脚本执行它时,AA[3] 不会 return 任意值!
这是我的脚本:
tot=["AA","AB","AC"];
AA=["1","2","3","4","5","6","7"];
AB=["1","2","3","4","5","6","7"];
AC=["1","2","3","4","5","6","7"];
for (let i = 0; i < tot.length; i++)
{
tot[i]+'[2]';
}
您的代码没有输出任何内容,因为您没有要求它输出任何内容。还有那一行: tot[i] + '[2]' 是不正确的。你到底想在那里做什么。我将其更改为向每个元素添加“[2]”字符串,但似乎这不是您想要的。
tot=["AA","AB","AC"];
AA=["1","2","3","4","5","6","7"];
AB=["1","2","3","4","5","6","7"];
AC=["1","2","3","4","5","6","7"];
for (let i = 0; i < tot.length; i++)
{
tot[i]+='[2]';
}
console.log(tot)
你可以试试:
const tot = [
["1","2","3","4","5","6","7"],
["1","2","3","4","5","6","7"],
["1","2","3","4","5","6","7"]
];
for (let i = 0; i < tot.length; i++)
{
console.log(tot[i][2]);
}
变量不能像函数一样工作。不要在循环中写入变量,请尝试:
console.log(tot);
此外,我假设你打算做 var tot = new Array(AA, AB, AC);
var AA=["1","2","3","4","5","6","7"];
var AB=["1","2","3","4","5","6","7"];
var AC=["1","2","3","4","5","6","7"];
var tot= new Array(AA,AB,AC);
for (let i = 0; i < tot.length; i++)
{
tot[i].push("2")
console.log(tot[i]);
}
要遍历 tot 数组中的每个数组,您必须嵌套两个 for 循环,并且 console.log 嵌套 for 循环中的每个值:
const AA=["1","2","3","4","5","6","7"];
const AB=["1","2","3","4","5","6","7"];
const AC=["1","2","3","4","5","6","7"];
const tot=[AA,AB,AC];
const logArray = (tot) => {
for (let i = 0; i < tot.length; i++) {
console.warn(`tot[${i}] contains:`);
for (let j = 0; j < tot[i].length; j++) {
console.log(tot[i][j]);
}
}
};
I have an array (tot) with arrays in it
实际上这是不准确的。您有一个数组,其中包含不是数组的字符串值。数组中的字符串值与某些数组的名称相匹配。取而代之的是,您可能想使用数组对象,例如:
let AA=["1","2","3","4","5","6","7"];
let AB=["1","2","3","4","5","6","7"];
let AC=["1","2","3","4","5","6","7"];
let tot={AA,AB,AC};
let index = 5;
for (let key in tot) {
console.log(`${key}[${index}]: ${tot[key][index]}`);
}
我有一个数组 (tot
),里面有数组。
我需要检查 (tot
) 中的每个条目的值,这是通过在控制台键入 AA[3] 完成的,但是,当我从脚本执行它时,AA[3] 不会 return 任意值!
这是我的脚本:
tot=["AA","AB","AC"];
AA=["1","2","3","4","5","6","7"];
AB=["1","2","3","4","5","6","7"];
AC=["1","2","3","4","5","6","7"];
for (let i = 0; i < tot.length; i++)
{
tot[i]+'[2]';
}
您的代码没有输出任何内容,因为您没有要求它输出任何内容。还有那一行: tot[i] + '[2]' 是不正确的。你到底想在那里做什么。我将其更改为向每个元素添加“[2]”字符串,但似乎这不是您想要的。
tot=["AA","AB","AC"];
AA=["1","2","3","4","5","6","7"];
AB=["1","2","3","4","5","6","7"];
AC=["1","2","3","4","5","6","7"];
for (let i = 0; i < tot.length; i++)
{
tot[i]+='[2]';
}
console.log(tot)
你可以试试:
const tot = [
["1","2","3","4","5","6","7"],
["1","2","3","4","5","6","7"],
["1","2","3","4","5","6","7"]
];
for (let i = 0; i < tot.length; i++)
{
console.log(tot[i][2]);
}
变量不能像函数一样工作。不要在循环中写入变量,请尝试:
console.log(tot);
此外,我假设你打算做 var tot = new Array(AA, AB, AC);
var AA=["1","2","3","4","5","6","7"];
var AB=["1","2","3","4","5","6","7"];
var AC=["1","2","3","4","5","6","7"];
var tot= new Array(AA,AB,AC);
for (let i = 0; i < tot.length; i++)
{
tot[i].push("2")
console.log(tot[i]);
}
要遍历 tot 数组中的每个数组,您必须嵌套两个 for 循环,并且 console.log 嵌套 for 循环中的每个值:
const AA=["1","2","3","4","5","6","7"];
const AB=["1","2","3","4","5","6","7"];
const AC=["1","2","3","4","5","6","7"];
const tot=[AA,AB,AC];
const logArray = (tot) => {
for (let i = 0; i < tot.length; i++) {
console.warn(`tot[${i}] contains:`);
for (let j = 0; j < tot[i].length; j++) {
console.log(tot[i][j]);
}
}
};
I have an array (tot) with arrays in it
实际上这是不准确的。您有一个数组,其中包含不是数组的字符串值。数组中的字符串值与某些数组的名称相匹配。取而代之的是,您可能想使用数组对象,例如:
let AA=["1","2","3","4","5","6","7"];
let AB=["1","2","3","4","5","6","7"];
let AC=["1","2","3","4","5","6","7"];
let tot={AA,AB,AC};
let index = 5;
for (let key in tot) {
console.log(`${key}[${index}]: ${tot[key][index]}`);
}