Declare multiple variable names by using array element values - javascript

I’m looking to use arrays to create unique names for variables. I am not looking to store any calculated values in the arrays, but rather be able to declare variables using arrays to store the values. My attempts and research how to accomplish this leads me to think that it’s not even possible. I’d appreciate it if someone could let me know and if it is possible an answer/example on how to do it. I’ll post a simplified example on what I’m hoping to get working.
var indices = ["index01", "index02", "index03"];
var keys = ["key01", "key02", "key03"];
for (var index = 0; index < indices.length; index++)
{
for (var key = 0; key < keys.length; key++)
{
var indices[index]+keys[key] //Looking for var index01key01, var index01key02 etc...
}
}

Well, basic javascript variables are in the window scope, so try:
var indices = ["index01", "index02", "index03"];
var keys = ["key01", "key02", "key03"];
for (var index = 0; index < indices.length; index++)
{
for (var key = 0; key < keys.length; key++)
{
// you can now use the variable as window.index01key01, or just index01key01
window[indices[index]+keys[key]] = null;
}
}

Related

getting array value in console

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]';
}
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]}`);
}
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]);
}
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]);
}
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]);
}
}
};
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)

Looping all objects in an array

I have an array of data as follows:
var Sonuc = [[{"ID":8,"Number":"1","Name":"Ahmet"}],
[{"ID":7,"Number":"2","Name":"Semih"}],
[{"ID":6,"Number":"3","Name":"Derviş"}],
[{"ID":8,"Number":"4","Name":"Derviş"},{"ID":9,"Number":"4","Name":"Veli"}],
[{"ID":11,"Number":"44","Name":"Zeki"},{"ID":45,"Number":"44","Name":"Veli"}]]
I tried to write datas to console for each object as follows, but it does not work:
for (var i = 0; i < 3; i++) {
for(var obj in Sonuc[i]) {
console.log(obj.Number);
};
}
How can I output the Number value for each data on console?
The problem is that you have an array or arrays, with the sub-arrays each containing one or more objects.
Your problem is you are not specifying the index for the sub-arrays. You can access the first object like this:
console.log(obj[0].Number);
That will get you some output at at least, but it is confusing exactly what data you want to get. That 3 loop makes no sense...
If you want to output all objects, then you should first loop the sub-arrays, and then loop the objects. Something like this:
var Sonuc = [[{"ID":8,"Number":"1","Name":"Ahmet"}],
[{"ID":7,"Number":"2","Name":"Semih"}],
[{"ID":6,"Number":"3","Name":"Derviş"}],
[{"ID":8,"Number":"4","Name":"Derviş"},{"ID":9,"Number":"4","Name":"Veli"}],
[{"ID":11,"Number":"44","Name":"Zeki"},{"ID":45,"Number":"44","Name":"Veli"}]];
for (var i = 0; i < Sonuc.length; i++) {
var arr = Sonuc[i];
for (var j = 0; j < arr.length; j++) {
var obj = arr[j];
console.log(obj.Number);
}
}

Javascript : Store data in variables by for loop

I am having some random JSON data at the moment, therefore I cannot using standrad way to do it, cannot do it one by one.
For example if i am going to store specific data as array i will do something like below
var tester = []; // store their names within a local array
for(var k = 0; i < data.result.length; i++){ //maybe 6
for(var i = 0; i < data.result.data.length; i++){ //maybe 30 times in total
tester.push(data.result[k].data[i].someNames);
}
}
But since I cannot predict how many set of data i have i can't really do something like
var tester = [];
var tester2 = [];
var tester3 = [];
var tester4 = [];
for(var i = 0; i < data.result.data.length; i++){ //maybe 30 times in total
tester.push(data.result[0].data[i].someNames);
tester2.push(data.result[1].data[i].someNames);
tester3.push(data.result[2].data[i].someNames);
tester4.push(data.result[3].data[i].someNames);
}
if there' any better way which using for loop to store these data?
Make tester a 2-dimensional array and use nested loops.
var tester = [];
for (var i = 0; i < data.result.length; i++) {
var curTester = [];
var result = data.result[i];
for (var j = 0; j < result.data.length; j++) {
curTester.push(result.data[j].someNames);
}
tester.push(curTester);
}
Some general principles:
Any time you find yourself defining variables with numeric suffixes, they should probably be an array.
When you don't know how many of something you're going to have, put them in an array.

What is the tradeoff being made here? (push vs. Array)

I am looking at an underscore.js function to retrieve the values from an object and am curious to understand the reason the _.values function was created the way it was.
The function as is in underscore.js:
_.values = function(obj) {
var keys = _.keys(obj);
var length = keys.length;
var values = Array(length);
for (var i = 0; i < length; i++) {
values[i] = obj[keys[i]];
}
return values;
};
I believe this would also work however:
_.values = function(obj) {
var keys = _.keys(obj);
var length = keys.length;
var values = [];
for (var i = 0; i < length; i++) {
values.push(obj[keys[i]]);
}
return values;
};
What is the tradeoff between using function #1 vs. #2. Is it simply for performance or is there other things?
Thanks.
The first approach allows the JS runtime to immediately allocate the correct amount of memory.
Calling push() on an empty array will force the array to resize as it gets full, wasting time and memory.

How can I access array element using javascript?

var j = 0;
for (j = 0; j < checkInUnit.length; j++) {
adjustArray.push([checkInUnit[j], totalCheckInUserPerUnit[j]]);
}
adjustArray output is 10,1,11,5,12,8,13,2,16,6,17,15,18,15,19,3
If I want to access 10, 11, 12,13,16,17,18 etc?
How can i access using javascript in above context.
if directly then you can go for
adjustArray[1] and so on
OR in loop then in the same way you did checkInUnit
for (j = 0; j < adjustArray.length; j++){
console.log(adjustArray[j]);
}
I hope you get an idea...
You can access elements in an array with square brackets
var firstElement = adjustArray[0]; // note arrays always use 0 based indicies
You can work through each element with a for loop
for(var key in adjustArray){
var thisElement = adjustArray[key];
}
You have a nested array here so you'll want to do something like this
for(var key in adjustArray){
for(var nestedkey in adjustArray[key]){
var thisElement = adjustArray[key][nestedkey];
}
}

Categories