How to include an array of data in an $http request using AngularJS

I am trying to store data in an array to the $http service in AngularJS. The way I am currently adding them to the service is as follows (and it is functioning correctly):

var inputValueArray  = new Array($scope.formdata);

Currently, I am able to retrieve data using index zero in my API. However, I want to be able to retrieve data using a named index. So, I tried the following:

var inputValueArray  = new Array();
inputValueArray["myIndex"] = $scope.formData;

Unfortunately, when I try to retrieve data in the API, I get an empty array. I then attempted the following solution:

var inputValueArray = {"myIndex" : $scope.formData};

Now, I am encountering a Maximum call stack size exceeded error. In both cases, I am using $_POST to receive the data. I am sending the data using $.param() like this:

data: $.param({ mydata:inputValueArray , csrf_token: myTokenKey})

Any suggestions or ideas would be greatly appreciated.

Answer №1

In the ECMAScript 2015 (ES6) standard, javascript includes a Map implementation.

Visit this link

For example:

var myMap = new Map();

var keyString = "a string",
    keyObj = {},
    keyFunc = function () {};

// Assigning values
myMap.set(keyString, "value associated with 'a string'");
myMap.set(keyObj, "value associated with keyObj");
myMap.set(keyFunc, "value associated with keyFunc");

myMap.size; // 3

// Retrieving the values
myMap.get(keyString);    // "value associated with 'a string'"
myMap.get(keyObj);       // "value associated with keyObj"
myMap.get(keyFunc);      // "value associated with keyFunc"

myMap.get("a string");   // "value associated with 'a string'"
                         // because keyString === 'a string'
myMap.get({});           // undefined, because keyObj !== {}
myMap.get(function() {}) // undefined, because keyFunc !== function () {}

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

What is the best way to calculate the total sum of the first element in each index of an array when using ng

I am looking to calculate the sum of the first elements from all indexes of an array using ng-repeat in AngularJS. My goal is to declare a variable and increment its value with each iteration of ng-repeat, then display that variable at the end. Below is ...

Tips for displaying HTML content in an AJAX success alert message with ASP.NET MVC and jQuery

I have an action result that sends content in the following format: public ActionResult MyAction() { string mystring = //doing something return Content(mystring , "html"); } Client Side: $.ajax({ url: "/MyController ...

Overlaying images on top of text

Is there a way to overlay an image on text, like a pattern image? Similar to applying color with a hex value in CSS: color: #ffffff; Any ideas on how this can be achieved? I want to have a pattern over text. https://i.sstatic.net/JaNZU.jpg Appreciate ...

Ways to host static content in ExpressJS for specific directories and exclude others

I need to configure my ExpressJS server to serve static files from specific paths only, excluding others. For example, I want to serve static files from all paths except /files where I only need to manipulate a file on the server. Currently, my code looks ...

Utilizing highcharts to visualize non-linear time data pulled from a CSV file

I am seeking guidance on implementing a simple graph based on data from a CSV file in web development. I lack experience in this area and have struggled to find a suitable example to follow. The CSV file contains data in the format of a unix timestamp, hu ...

Ionic ion-view missing title issue

I'm having trouble getting the Ionic title to display on my page: http://codepen.io/hawkphil/pen/oXqgrZ?editors=101 While my code isn't an exact match with the Ionic example, I don't want to complicate things by adding multiple layers of st ...

Merging an AppBar and Drawer in Material UI for a seamless user interface design

I am working on integrating an AppBar component with a drawer feature. Here is the code for the AppBar: import React from "react"; import PropTypes from "prop-types"; import { withStyles } from "material-ui/styles"; import AppBar from "material-ui/AppBar" ...

JSON API WebConnector for Tableau

After creating a tableau webconnector to extract data from an internal API, I used earthquakeUSGS.html as a reference (https://github.com/tableau/webdataconnector). The API responds with json data (see code below). Utilizing the "Web data connector simulat ...

What is the general consensus on combining SWR with Redux - is it considered a best practice?

I am currently incorporating both SWR and Redux into my code. Here is how I'm using them together: const vehiclesStates = useSelector(({ vehiclesStates: state }) => state); // REDUX const response = useFetch("/vehicles/states") // SWR con ...

Refreshing UI components using a filter in Angular

I am currently working with a list that uses data-ng-repeat to display elements and filters based on the displayed items. <ul> <li data-ng-repeat="topic in topics | belongs:projectID"> <span>{{device.name}} </span><a d ...

Unable to link to 'mdMenuTriggerFor' as it is not a recognized attribute of 'button'

During the execution of my angular application using ng serve, I encounter the following error: Can't bind to 'mdMenuTriggerFor' since it isn't a known property of 'button' Despite importing all the necessary components, I a ...

What is the best way to determine the size of a JSON array that was returned from a server?

I have a JSON array containing objects within an array. I'd like to know how to determine the length of keys and values inside each object. Can anyone assist me in figuring out how to get the length of an object within an array? While I can obtain the ...

Describing a function in Typescript that takes an array of functions as input, and outputs an array containing the return types of each function

Can the code snippet below be accurately typed? function determineElementTypes(...array: Array<(() => string) | (() => number) | (() => {prop: string}) | (() => number[])>) { /// .. do something /// .. and then return an array ...

Ways to connect the value of one variable to multiple others

I am trying to work with two variables: masterdata and datatoedit. The masterdata variable contains an array value, while datatoedit holds an object value. My goal is to copy the first data element from masterdata into the variable datatoedit, so I attemp ...

Guidelines for ASP.NET MVC 4: Delivering a file from a controller to a view using AngularJS

I am having trouble downloading a file from the server, despite following various examples. Here is the code snippet that I have been using: Controller (ASP NET MVC): public HttpResponseMessage GetFile(string filename) { try ...

Error encountered in clientside js file: Node.js indicates that 'require' is not defined

In my JavaScript file, I have important data that needs to be saved into a Mongoose schema and then inserted into a MongoDB table. The schema is stored in a separate directory, so I attempted to import it by adding the following line at the beginning of th ...

Merging JSON objects within an array using Node.js

When calling a function that returns an array with JSON objects, I encountered this issue: return new Promise((resolve) => { fileImport(userImport) .then ((response) => { console.log(response); resolve(response); }) }) result: ...

Mongoose consistently fails to properly save dates

I have created a Mongoose model and included a birthdate field in the following way: birthdate: { type: Date, required: [true, "Please enter a birthdate"], lowercase: true, validate: [isDate, "Please enter a valid birthdate&q ...

Consistently directing to a single page on the Node Js web server

I'm in the process of creating a website with multiple HTML pages. Currently, I have code that successfully links to the login page, but unfortunately, the localstores page also directs to the login page. const express = require('express') ...

How can I use jQuery UI to slide a div, while also smoothly moving the adjacent div to take its place?

Wishing you an amazing New Year! I am looking to create a smooth sliding effect for a div when a button is clicked. I want the adjacent div to slide alongside it seamlessly, without any clunky motions or delays. Currently, the adjacent div only moves afte ...