0

I have the following in apps script:

function getQAs() {
    return [

      { "Do you have any pictures ?|1 ": {"yes":2,"no":3 } },
      { "Do you have any pictures ?|2 ": {"yes":2,"no":3 } },
      { "Do you have any pictures?|3 ": {"yes":2,"no":3 } },
    ]
}

I'm trying to build a function that will search through the keys of The objects for a number. I'm testing with the number 1 .When I run:

function testQA() {
  var qa = getQAs();
  var matches = qa.keys().filter(function(row) { //ONLY CHECKED ROWS.
    Logger.log(row)
    return row.indexOf('1') == true;
  });

  Logger.log(matches);
}

I get

JS: TypeError: Cannot find function keys in object . What am I doing wrong?

10
  • 2
    keys is not a prototype method. It only exists on the 'static' Object class. So you would need Object.keys(qa) Commented Mar 4, 2019 at 17:10
  • 1
    You are executing keys() on an array Commented Mar 4, 2019 at 17:11
  • 1
    @AnuragSrivastava Which would be totally okay because there is a .keys() method defined for Array (Array.prototype.keys()) ;) Commented Mar 4, 2019 at 17:14
  • 1
    @AnuragSrivastava I didn't say that it makes a lot more sense with the function, only that it would be okay (at least in browsers that support Array.prototype.keys()) :) Commented Mar 4, 2019 at 17:18
  • 2
    Why use such a weird format that requires parsing. Got an object, make it useful. Commented Mar 4, 2019 at 17:26

2 Answers 2

1

You need to use a for...in loop to get the keys of an object. I devised a simple loop through keys to determine if the value existed within the key, then pushed out a filtered array

function testQA() {
  var qa = getQAs();

  function getRow(row_identifier) {
  var filtered = [];
    qa.forEach(function(v) {
      for(var k in v) {
       if(k.indexOf(row_identifier) > 0) filtered.push(v);
      }
    });
         return filtered;
  }

   return getRow(row_identifier);
}

function getQAs() {
  return [

    {
      "Do you have any pictures ?|1 ": {
        "yes": 2,
        "no": 3
      }
    },
    {
      "Do you have any pictures ?|2 ": {
        "yes": 2,
        "no": 3
      }
    },
    {
      "Do you have any pictures?|3 ": {
        "yes": 2,
        "no": 3
      }
    },
  ]
}

function testQA() {
  var qa = getQAs();

  function getRow(row_identifier) {
  var filtered = [];
    qa.forEach(function(v) {
      for(var k in v) {
       if(k.indexOf(row_identifier) > 0) filtered.push(v);
      }
    });
         return filtered;
  }
      console.log(getRow(1));
      console.log(getRow(2))
      console.log(getRow(3))
}
testQA();

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

1 Comment

Apps Script is basically JS 1.6. No includes, no let, no iterators, no arrow syntax. It does have map, filter, reduce, some, and every support.
1

Just playing around with your question

I'm not that good with objects so I thought it would be fun to play around with this and see what I can learn. I have no expectation of having my answer chosen, I just thought it might be valuable to a less experienced programmer, like myself.

function getQAs() {
  var qa=[{"Do you have any picture?|1":{yes:"2",no:"3"}},{"Do you have any picture?|2":{yes:"4",no:"5"}},{"Do you have any picture?|3":{yes:"8",no:"9"}}];
  return qa;
}

function testQA(row) {
  var row=row || 3;
  var qa = getQAs();
  Logger.log(qa);
  var qA=[];
  for(var i=0;i<qa.length;i++) {
    qA.push(Object.keys(qa[i]));
  }
  Logger.log(qA);
  for(var i=0;i<qA.length;i++) {
    if(qA[i].toString().indexOf(row)>-1){
      var rv=JSON.stringify(qa[i]);
      var t1=qA[i];
      var t2=qa[i];
      var yeses=t2[t1].yes;
      var nos=t2[t1].no;
      var t3={yes:yeses,no:nos};
      return t3;
      break;
    }
  }
}      

Hopefully, someone can learn something from this by single stepping through the program. It returns an object with the number of yeses and noes for the selected object.

Comments

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.