-3

I have an array (tot) with arrays in it. I need to check each entry in (tot) for a value , which is done at console by typing AA[3] , however, when I execute it from a script, AA[3] would NOT return any value!

here is my script:

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]';
}
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
FooDue
  • 3
  • 4
  • [“Variable” variables in JavaScript](https://stackoverflow.com/q/5187530) – VLAZ Aug 30 '21 at 09:21
  • What do you want to do with this value? Your loop doesn't return or assign anything. – Ivar Aug 30 '21 at 09:25
  • i need to display every value in tot followed by a set value to check if it has it, say i need position 5, i type AA[5] in console, and i either get a value or undefined, so i need to test each under tot for 5, AA[5], AC[5] and so on – FooDue Aug 30 '21 at 13:49

5 Answers5

1

I have an array (tot) with arrays in it

Actually that's inaccurate. You have an array with string values inside that are not arrays. The values of the strings you have in your array match the names of some arrays. Instead of this you might want to use an object of arrays, like:

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]}`);
}
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
0

You could try:

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]);
}
tdranv
  • 1,140
  • 11
  • 38
0

Variables do not work as functions. Instead of writing variables in a loop, try:

console.log(tot);

Also, I assume you meant to do 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]);
}
New Programmer
  • 108
  • 1
  • 1
  • 9
0

To loop through every array inside tot array, you have to nest two for loops, and console.log each value in nested for loop:


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]);
    }
  }
};
MichalRsa
  • 181
  • 1
  • 4
-1

Your code doesn't output anything, because you didn't ask it to. also that line: tot[i] + '[2]' is not correct. What exactly are you trying to do there. I changed it to add '[2]' string to each element, but doesn't seem that that is what you want.

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)
J_K
  • 688
  • 5
  • 12