-2

I am trying to write a function that finds the biggest value among JSON arrays and then returns another value from the array where the biggest value was found. In concrete: Find the table with the highest (i.e. most recent) creationTime and then return the id of that table. The function should return "clickup-test-example:ClickUp_Teams.ClickUp_Teams_1619170399502"

{
    "totalItems": 3,
    "kind": "bigquery#tableList",
    "etag": "PCARXzLSMg+ycECbmPnMDQ==",
    "tables": [
      {
        "kind": "bigquery#table",
        "id": "clickup-test-example:ClickUp_Teams.ClickUp_Teams_1618403016322",
        "tableReference": {
          "projectId": "clickup-test-example",
          "datasetId": "ClickUp_Teams",
          "tableId": "ClickUp_Teams_1618403016322"
        },
        "type": "TABLE",
        "creationTime": "1618403016433",
        "expirationTime": "1623587016433"
      },
      {
        "kind": "bigquery#table",
        "id": "clickup-test-example:ClickUp_Teams.ClickUp_Teams_1619168558388",
        "tableReference": {
          "projectId": "clickup-test-example",
          "datasetId": "ClickUp_Teams",
          "tableId": "ClickUp_Teams_1619168558388"
        },
        "type": "TABLE",
        "creationTime": "1619168558487",
        "expirationTime": "1624352558487"
      },
      {
        "kind": "bigquery#table",
        "id": "clickup-test-example:ClickUp_Teams.ClickUp_Teams_1619170399502",
        "tableReference": {
          "projectId": "clickup-test-example",
          "datasetId": "ClickUp_Teams",
          "tableId": "ClickUp_Teams_1619170399502"
        },
        "type": "TABLE",
        "creationTime": "1619170399591",
        "expirationTime": "1624354399591"
      }
    ]
}

I tried writing this function based on these two threads: Getting max value(s) in JSON array and Finding the max value of an attribute in an array of objects , but I don't know how to write the Math.max function. Any other solution would be appreciated as well. Thanks in advance!

function findNewestTables() {
  const tables = BigQuery.Tables.list('clickup-test-307314','ClickUp_Teams'); // returns the JSON file above
  const newestTables = Math.max(tables.tables.map(function(o) { 
    return o.id;
    }));

  console.log(tables);
  console.log(newestTables);

};

2
  • 1
    You linked to another question with math.max with an array, but you do not do it the way that was answered? Commented Apr 23, 2021 at 18:59
  • The difference between Java and JavaScript Commented Apr 23, 2021 at 19:01

3 Answers 3

1

You can just track the maxSoFar and id in separate variables, and when the maxSoFar changes, so should the id.

// Gets the original JSON, and declares the variables
const data = getData();
let
  maxSoFar = 0,
  id = "";

// Updates the variables whenever a newer item is found
data.tables.forEach(table => {
  const time = parseInt(table.creationTime);
  if(time > maxSoFar){
    maxSoFar = time; 
    id = table.id;
  }
});
// Logs the result
console.log(id);

// Provides the original JSON
function getData(){
  return {
    "tables": [
      { "id": "Teams_1618403016322", "creationTime": "1618403016433" },
      { "id": "Teams_1619168558388", "creationTime": "1619168558487" },
      { "id": "Teams_1619170399502", "creationTime": "1619170399591" }
    ]
  }
}

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

Comments

1

One way to solve this problem would be to just loop through the tables, search for the table with the largest creationTime, and then return the ID that belongs to that table.

Here's an example of a rough implementation:


const findBiggestInTable = (tables) => {
  const largest = tables[0];
  for (int i = 0; i < tables.length; i++) {
    if (tables[i].creationTime > largest.creationTime) {
        largest = tables[i];
    }
  }

  return largest.id;
}

1 Comment

This works great if you just change const largest = to let largest = (since you're rebinding that identifier in the for loop.)
1

There's probably a clever way of doing this as a one-liner, but maybe …

function findMostRecent(json) {
  const times = json.tables.map(table => table.creationTime);
  const maxValue = Math.max(...times);
  const mostRecent = x.tables.find(table => table.creationTime === maxValue);
  
  return most.recent.id
}

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.