0

I'm friendly with python, but I need the same code in google apps script. ( the code is under the text)

Thank you in advance

I need my output like this :

plus = [[600, 'ter', 'simple'], [600, 'ter', 'simple']]

Here's my code :

      function myFunction() {
  
          var data =[[600, 'ter', 'simple'], [600, 'ter', 'simple'], [300, 'ter', 'medium'], [200, 'ter', 'Hard']]

          var plus  = []

          for (let i = data[0]; i < data.leng; i++)
          {
            if ( data[i][0] == data[0][0])
            {
              plus.push(data)
            }
          }
        }
Riad
  • 31
  • 4
  • There is no tuple in Google Apps Script (js) that you need to use 2d array in the format like `[[], []]`. – idfurw Dec 24 '21 at 09:46
  • From the [javascript tag info](https://stackoverflow.com/tags/javascript/info): "*[JavaScript] is unrelated to the Java programming language and shares only superficial similarities. ...*" – Turing85 Dec 24 '21 at 09:48
  • Thank you so much @Turing85, I've edit my message – Riad Dec 24 '21 at 09:59
  • You should just modify the final line to `plus.push(data[i])`. Also see https://stackoverflow.com/questions/63720612/what-does-the-range-method-getvalues-return-and-setvalues-accept – TheMaster Dec 24 '21 at 10:29

2 Answers2

0

Couple of syntax mistakes here and there. Pointed them in code:

/*<ignore>*/console.config({maximize:true,timeStamps:false,autoScroll:false});/*</ignore>*/
function myFunction() {
  var data = [
    [600, 'ter', 'simple'],
    [600, 'ter', 'simple'],
    [300, 'ter', 'medium'],
    [200, 'ter', 'Hard'],
  ];
  var plus = [];
  for (
    let i = 0 /* intializer or counter should start with 0 not data[0]  */;
    i < data.length /* No property named leng data.leng */;
    i++
  ) {
    if (data[i][0] == data[0][0]) {
      plus.push(
        data[
          i
        ] /* push only the current element  `data[i]` and not the full `data` array */
      );
    }
  }
  console.log(plus);
}
myFunction();
<!-- https://meta.stackoverflow.com/a/375985/ -->    <script src="https://gh-canon.github.io/stack-snippet-console/console.min.js"></script>
TheMaster
  • 45,448
  • 6
  • 62
  • 85
  • So glad you're in this forum @theMaster, thank you so much for the code and also for the comment, that's help me a lot to understand a lot of basics things. I always learn from you, you're the best. – Riad Dec 24 '21 at 10:47
0

Use Array.filter(), like this:

function filterData() {
  const data = [
    [600, 'ter', 'simple'],
    [600, 'ter', 'simple'],
    [300, 'ter', 'medium'],
    [200, 'ter', 'Hard'],
  ];
  const filterBy = { column: 0, value: data[0][0] };
  return data.filter(row => row[filterBy.column] === filterBy.value);
}

doubleunary
  • 13,842
  • 3
  • 18
  • 51