Home > database >  Looping over an object is not taking element by element in apps script
Looping over an object is not taking element by element in apps script

Time:11-30

I have an array and a dictionary object, I want to check if the elements in the array are present in the object key and if not present then move the last not matched value from object into a new array.

If the last value itself is a match then move the value in another array

array is

var Concat_names = [a, b, c]

object is

var Concat_rows = {a=D45, d=D64, e=D72, f=D90}

I tried the below

var Last_CellnoMatch = []
var Lact_CellisMatch = []

  for (var x in Concat_names){
    for (var key in Concat_rows){
      if(key!=Concat_names[x]){
        Last_CellnoMatch.push(Concat_rows[key]); 
        Logger.log(Last_CellnoMatch);
      }  
    }
  }
Logger.log(Cell);

I want Last_CellnoMatch to get the last value in the object where its a no match as "D90"in to and if last value is a match then move the last value into a new array Lact_CellisMatch

Please help me achieve this, Thank you!

CodePudding user response:

First of all, your object is built wrong. I scribbled a possible solution for you:

var Concat_names = ["a", "b", "c"];
var Concat_rows = {a: "D45",d: "D64",e: "D72",f: "D90"};
var Last_CellnoMatch = [];
var Last_CellMatch = []

  var keys = Object.keys(Concat_rows);
  var last_value_match = "";
  var last_value_no_match = "";
  for (var key of keys) {
    if (Concat_names.indexOf(key) == -1) {
      last_value_no_match = Concat_rows[key];
    } else {
      last_value_match = Concat_rows[key];
    }
  }
  if (last_value_no_match != "") Last_CellnoMatch.push(last_value_no_match);
  if (last_value_match != "") Last_CellMatch.push(last_value_match);
  console.log(Last_CellnoMatch);
  console.log(Last_CellMatch);

  • Related