What could be causing the .map function to return arrays with undefined objects?

I've been working on fetching JSON data from my API to import it into Google Sheets using Google Apps Script. Everything is running smoothly, except for one issue - when I try to map the object, the resulting array contains undefined objects.

Here's the snippet of my script:

function GETMYJSONDATA() {

  var options = {
    "method" : "get",
    "headers" : {
    "Authorization": "Bearer REDACTED"
  }
};

const url = "REDACTED"
const res = UrlFetchApp.fetch(url, options)
const dataAsText = res.getContentText()
const data = JSON.parse(dataAsText)

const results = data.response.users.map (user => {
  return
    [user["department_text"]]
})
  return results
}

​Here you can see the raw JSON response from my API and also the result from the Apps Script debugger:

https://i.sstatic.net/oIFIX.png

https://i.sstatic.net/tWK57.png

Answer №1

The following code snippet:

return
[user["department_text"]]

Should be condensed to a single line like this:

return [user["department_text"]]

Answer №2

The reason for the issue is that the desired value to be returned is placed on a separate line following the return statement:

return
[user["department_text"]]

If you adjust it to be on the same line, it will return the correct value.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Here's a step-by-step guide on how to parse JSON information in JavaScript when it's formatted as key-value

I need to parse the JSON data in JavaScript. The data consists of key-value pairs. Data looks like this: {09/02/2014 15:36:25=[33.82, 33.42, 40.83], 08/11/2014 16:25:15=[36.6, 33.42, 40.45], 07/30/2014 08:43:57=[0.0, 0.0, 0.0], 08/12/2014 22:00:52=[77.99 ...

What is the process for modifying a value within an array in JSON data?

Here is the JSON Data: { "role": [ "Jungle", "Mid" ], "total_wins": 0 } This particular request is made using npm request. request({ uri: uri, method: "PATCH", json: { "total_wins": 1, "rol ...

The component triggering the redirect prematurely, interrupting the completion of useEffect

I set up a useEffect to fetch data from an endpoint, and based on the response, I want to decide whether to display my component or redirect to another page. The problem I'm facing is that the code continues to run before my useEffect completes, lead ...

Error: Google Chrome encountered an unexpected token } that caused a syntax error

I encountered an error that reads: Uncaught SyntaxError: Unexpected token } This error only appears in Chrome, while other browsers like Mozilla and IE do not show it. Here is my script causing the issue: <script type="text/javascript" language="jav ...

JavaScript's functionality akin to PHP's exec() function

I am searching for a way to run a shell command through javascript, similar to the functionality of PHP's "exec()" function. I understand that executing shell commands in javascript may not be recommended due to security concerns. However, my javascri ...

outputting a sequence by using a for-loop

I created an array containing 10 multiples of 7 and am attempting to display it in reverse order using a for loop. However, the code doesn't seem to be working as expected. I have successfully printed the array in normal order using a for or for-each ...

Guide to generating a multidimensional array from a single array in PHP

If we consider an array like this: array(1,2,3,4,...) I am in need of converting it to: array( 1=>array( 2=>array( 3=>array( 4=>array() ) ) ) ) Seeking assistance on this task. ...

A commitment was formulated within a handler but failed to be returned from it

After a user clicks on the button (#lfdsubmit), it triggers the function (LFD_SearchContainer()) which is expected to return a promise. However, errors are occurring at LFD_SearchContainer('EISU1870725') .then(container => { ST2.db2(contai ...

Automatically update div content using AJAX in a different approach

In my situation, I am facing a unique challenge compared to other queries. I have a div element with the following code <div id="ondiv"><?php ?></div> Within this PHP section are details of people who are currently online. Ideally, when ...

Steps to retrieve specific text or table cell data upon button click

Greetings, I am a beginner in the world of html and javascript, so please bear with me :) My challenge involves working with a table in html. Each row contains a dropdown menu (with identical options) and a button. When the button is clicked, I aim to sen ...

Executing test spec before promise resolution in $rootScope.$apply() is completed

When writing angular unit tests using Jasmine with angular-mocks' httpBackend, I successfully mocked my backend. However, one of my tests is encountering issues with an http call where a value is set to scope after the request is complete (in then()). ...

What steps can be taken to hide empty items in a list from being shown?

When I map over an array of books to display the titles in a list, I encounter an issue with the initial empty string value for the title. This causes the list to render an empty item initially. Below is my book array: @observable books = [ {title:" ...

Tips for populating class attributes from an Angular model

Suppose there is a Class Vehicle with the following properties: public id: number; public modelId: number; public modelName: string; Now consider we have an object that looks like this {id: 1, modelId: 1, modelName: "4"} What is the best way to assign e ...

Troubleshooting problem with displaying child component in Vue's nested routes

I am facing an issue with vue-router nested routes. https://router.vuejs.org/guide/essentials/nested-routes.html I created a parent route User and a child route UserQuotes, but the child route is not rendering. There are no error messages or warnings in ...

What is the method for creating a random percentage using a dynamic array?

Consider the following dataset: var datas = [{animal:"chicken"}, {animal: "cow"}, {animal: "duck"}]; var after_massage = []; datas.forEach(function(key){ after_massage.push({animal: key.animal}, {percentage: randomPercent(); }) }) I'm current ...

The innerHTML property and &nbsp; entity

I'm encountering a strange JavaScript behavior that has me puzzled. Take a look at this code snippet: var el = document.createElement('div') var s = String.fromCharCode(160) el.innerHTML = s console.log(s) // prints space cons ...

What is the best way to send multiple requests until one is successful in Node.js without causing delays?

My situation involves a function that requires a parameter and a callback. This function is responsible for making a request to an external API in order to retrieve information based on the parameter provided. The challenge arises when the remote API occas ...

"Effortlessly Engage Users with Rails and AJAX Comment Posting

Running a simple blog app dedicated to video game reviews, I encountered an issue with AJAX. As a self-taught student developer, I'm facing a challenge where posting comments on review pages triggers a full page refresh instead of dynamically updating ...

Failed browser extension popup launch

I've learned that popups are created using an HTML file. Here is the code I have written for a popup window, but unfortunately it does not open when I click the icon. Any suggestions on what might be causing this issue? { "name": "Popup Snake", ...

Easiest method to find the longest word in a string with JavaScript

Discover the Lengthiest Word in a Sentence: function searchForLongestWordLength(sentence) { return Math.max(...sentence.split(" ").map(word => word.length)); } searchForLongestWordLength("The quick brown fox jumped over the lazy dog"); ...