0

newbie question:

i am using a simple for loop in my code. Let's say this returns 3 pozitive values of a, b and c. What i want is to have all 3 values described as different variables to use later outside the loop. Is it possible? Sample code:

var CampAdGR = ss.getRange("A9").getValue();
var data = ss.getRange("A23:A").getValues();

for (var i = 0; i < data.length; i++) {
    if (data[i][0] == CampAdGR) {*continue code*}

Thanks!

6
  • So you want to loop through data, and every value needs to be stored in a seperate variable? Commented Dec 12, 2018 at 13:05
  • That's exactly what i need. Commented Dec 12, 2018 at 13:18
  • I posted an answer, I don't know why you would need every value in a separate variable but I wrote the loop in my answer. Hope this helps. Commented Dec 12, 2018 at 13:56
  • I need it because later in my script i use another for loop, so i don't want it to be looped with every value of the first loop. Commented Dec 12, 2018 at 14:27
  • If you use alot of loops, look into the array functions. So if you want to loop over a smaller subset of the original data, you can use Array.filter() to create a new array to loop over without the hassle of having another variable to store the results, another var i to keep track of the index etc. So you can probably do stuff like: data.filter(item => item[0] === CampAdGR).forEach( ... do stuff with only CampAdGR items ... ); Commented Dec 12, 2018 at 15:57

3 Answers 3

1

If you have an array of values, you can either declare the variables seperately:

var data = [ 1, 2, 3 ];
var a = data[ 0 ];
var b = data[ 1 ];
var c = data[ 2 ];
console.log( a );
console.log( b );
console.log( c );

Or you could use destructuring to set them in one line:

const data = [ 1, 2, 3 ];
const [ a, b, c ] = data;
console.log( a );
console.log( b );
console.log( c );

If you don't know in advance how many values you'll have inside the data, it's better to just keep working with the entire data array, since transforming the data can be done with array.map(), array.forEach() etc.

Sign up to request clarification or add additional context in comments.

Comments

0

You can declare a different variable, usually an array, and use it as a storage.

var CampAdGR = ss.getRange("A9").getValue();
var data = ss.getRange("A23:A").getValues();
var storage = [];

for (var i = 0; i < data.length; i++) {
    if (data[i][0] == CampAdGR) {
        storage.push(data[i]);
    }
}

storage.map( storedValue => {
   console.log(storedValue);
});

If you need 3 different storage, declare 3 variables or save them on a single object.

Comments

0

The loop will create new variable names at run time (varName1, varName2, varName3...). I concatenate the index i with the varName for unique variable names (it seems logic to me because the index is the only value changing throughout the loop).

The output will give you n times new variables (depending on how big your data array is) with their corresponding values.

var CampAdGR = ss.getRange("A9").getValue();
var data = ss.getRange("A23:A").getValues();

for (var i = 0; i < data.length; i++) {
    if (data[i][0] == CampAdGR) {
        var varName = varName.concat('aName' + data[i]);
    }
}

4 Comments

This would look like the perfect solution, but i get this error on Your added line: Missing ; before statement.
I edited my answer, should work now. If you have the good result, please mark as answered and up vote. Thanks
I tried adding ; but it still throws the same error.
This is invalid syntax written this way and currently impossible in JS. Besides, Why would you want this? If you need to directly reference something in the array, you can create an object from the array using array.reduce() and use the object properties, which can be someName + i, for direct reference. But that only works if you know the amount of items and then destructuring is way easier and shorter to write. If you don't know the amount of values, you would have to loop over the object keys again to know what they are called, defeating the purpose of creating the direct reference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.