WC123
WC123

Reputation: 97

Javascript loop through an array of objects and return the first value

I have created a function that is supposed to loop through an array of objects and return the first value of each object.

function getSheetSectionData(name){
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(name);
  var sheetData = sheet.getDataRange().getValues();
  
  var data = [];
  
  for (var i = 0; i < sheetData.length; i++){
      var obj = {};
      obj = sheetData[i][0];
      return sheetData[i][0];
  }
      data.push(obj);
}

It's only returning the first item in the first row/column. Any clues on what I'm missing?

Upvotes: 1

Views: 4594

Answers (4)

Affes Med Fedi
Affes Med Fedi

Reputation: 57

You have to move the return statement outside the loop.

function getSheetSectionData(name){
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(name),
  sheetData = sheet.getDataRange().getValues(),
  data = [];
  
  for (var i = 0; i < sheetData.length; i++){
      var obj = {};
      obj = sheetData[i][0];
      data.push(obj);
  }
   return data; 
}

Upvotes: 0

Eduard Malakhov
Eduard Malakhov

Reputation: 1114

I'm not sure what your intent is, but probably it should be something like this?

function getSheetSectionData(name){
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(name);
  var sheetData = sheet.getDataRange().getValues();

  var data = [];

  for (var i = 0; i < sheetData.length; i++){
      var obj = {};
      obj = sheetData[i][0];
      data.push(obj);
  }

  return data;
}

UPDATE

As @grogx noted below, creation of a temporary object appears unnecessary in this context and the sample above could be optimized to

function getSheetSectionData(name){
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(name);
  var sheetData = sheet.getDataRange().getValues();

  var data = [];

  for (var i = 0; i < sheetData.length; i++){
      data.push(sheetData[i][0]);
  }

  return data;
}

Which can further be shortened to

function getSheetSectionData(name){
    return SpreadsheetApp.getActiveSpreadsheet()
        .getSheetByName(name)
        .getDataRange()
        .getValues()
        .map((e) => e[0]);
}

However, we do not really know, what the original intent of the OP was. It may be the case, that that temporary object was indeed required for some sort of intermediate transformation, which was striped out from the MCVE.

Upvotes: -1

HenryDev
HenryDev

Reputation: 4953

How about this solution. Hope it helps!

var sheetData = [{name : "Mike", id: 10},{name : "Laura", id: 23},{name : "carl", id: 25},{name : "Lori", id: 23}];
    var arr = []
    for(var i in sheetData){
        var someObject = sheetData[i];
        arr.push(someObject[Object.keys(someObject)[0]]);
    }
    console.log(arr);

Upvotes: 0

kind user
kind user

Reputation: 41893

You could use Object.keys together with Array#map to get just the first key value from each object.

data = sheetData.map(v => v[Object.keys(v)[0]]);


Working example:

var arr = [{foo: 'bar', bar: 'foo'},{foo: 'war', bar: 'foo'},{foo: 'mar', bar: 'foo'}],
    res = arr.map(v => v[Object.keys(v)[0]]);
    
    console.log(res);

Upvotes: 2

Related Questions