Home > Software engineering >  Iterate over an object where the condition is less than the increment
Iterate over an object where the condition is less than the increment

Time:11-30

I have an array CELL = [abc, def, xyz]

and i have two variables CONST START_LINE = 45 and CONST COLUMN = 'D'

I am trying to get iterate over CELL to get a new object as To_include = {'abc' : D45, 'def':D61, 'xyz':77}

I tried the below but not getting the right output

Concat_rows_array =[];
To_add = {};
  if (Concat_rows_array.length==0){
    for (var i = 0; i <= CELL.length; i  = 16){
      Temp_START_LINE = i   START_LINE;
      To_add[Cell] = COLUMN Temp_START_LINE;
    }
  }
  Logger.log(To_add);

I am new to this, please help me here.

CodePudding user response:

Modification points:

  • If To_add is To_include, it is required to modify it.
  • If your variables are const CELL = ["abc", "def", "xyz"];, const START_LINE = 45;, const COLUMN = 'D';, To_add[Cell] is required to be modified like To_include[CELL[i]].
  • In the case of for (var i = 0; i <= CELL.length; i = 16){,,,}, it is required to be i < CELL.length.
  • If you want to retrieve To_include = {'abc' : D45, 'def':D61, 'xyz':77}, it is required to check the last element in the loop.

When these points are reflected in your script, how about the following modification?

Modified script:

const CELL = ["abc", "def", "xyz"];
const START_LINE = 45;
const COLUMN = 'D';

Concat_rows_array = [];
To_include = {};
if (Concat_rows_array.length == 0) {
  const len = CELL.length;
  for (var i = 0; i < len; i  ) {
    Temp_START_LINE = START_LINE   (16 * i);
    To_include[CELL[i]] = (i < len - 1 ? COLUMN : "")   Temp_START_LINE;
  }
}
console.log(To_include);

  • When this scrpt is run, To_include is {"abc":"D45","def":"D61","xyz":"77"}.
  • Related