Add an item to an array and then transform the array into a JSON format

I have a situation where I am creating an object and pushing it into an array. After that, I convert it into JSON format. When I display the dataCharts using an alert, it is returned in this form:

[{"AllLinks":"Link9","LinkURL":"url1"},{"AllLinks":"Link6","LinkURL":"url2"}]

But actually, I want it to be like this:

[{AllLinks:"Link9",LinkURL:"url1"},{AllLinks:"Link6",LinkURL:"url2"}]

The code I am using is as follows:

  $.ajax({

    url:  url,

    type: "get",

    headers: {"Accept": "application/json;odata=verbose"},

    success: function (data) {

      var array = [];
      
      for (var i=0; i < data.d.results.length; i++) {
        var item = data.d.results[i];
        
        array.push({
          AllLinks: item.AllLinks,
          LinkURL: item.LinkURL.Url
        });
      }

      dataCharts = JSON.stringify(array);     
      
      alert(dataCharts);
      
      AddDefaultLinks(dataCharts);

    },

    error: function (data) {

      alert(data.responseJSON.error);

    }

});

Answer №1

Tip:

[{"_aaaa_":"bbbb","_cccc_":"eeee"}]
[{aaaa:"bbbb",cccc:"eeee"}]

To eliminate the quotes from your label, follow these naming conventions:

_AllLinks_: it.AllLinks,  

_LinkURL_: it.LinkURL.Url

Then remove the underscores and quotes as shown below:

dataCharts.replace(/"_|_"/g,"")   

Just make sure it doesn't clash with other data (or have two _s)

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

Switch body class when the navbar's collapse show class is toggled

There are numerous methods to accomplish this task, but I am seeking the most optimal and efficient approach. My goal is to toggle a custom body class when the .navbar-toggle triggers the .show class on the .navbar-collapse element. I'm hesitant abo ...

Is it possible to notify the user directly from the route or middleware?

In my current setup, I am utilizing a route to verify the validity of a token. If the token is invalid, I redirect the user to the login page. I am considering two options for notifying users when they are being logged out: either through an alert message ...

Begin the initial function again once the second function has been completed

I have 2 functions in my code: function DisplayAltText(){ var CurrentPhoto = $("#DisplayPhoto img").attr("src"); if( $.browser.msie ) { IECurrentPhoto (CurrentPhoto); } if ($(".PhotoGallery img[src='" +CurrentPhoto+ "&a ...

Using forEach Loop with Promise.all in Node.js

I am seeking a solution for a task where I need to read a directory, copy its contents, and create a new file within that same directory. function createFiles(countryCode) { fs.readdir('./app/data', (err, directories) => { if (err) { ...

Displaying a dynamic view following several asynchronous ajax requests using Backbone

I am working with a backbone view and I have a scenario where I need to render HTML after two asynchronous calls have completed: initialize: function (model, options) { team.fetch({ success: function (collection) { ...

The way jQuery and CSS are rendered in the same browser window can vary depending on whether they are loaded via Django or statically

Experiencing a perplexing dilemma. While viewing a web page created using html, jquery, and css on the same browser but in two separate tabs, I am encountering varying renderings of the page. Scenario 1: Directly loading the html file from the file system ...

Verify the presence and delete a table row from the JSON data that is returned

Is there a way to verify the presence of a label in the JSON response and exclude it from the displayed row in my table? In the code snippet below, you can observe that I am returning 'Page Name not defined'. I want to hide this label and any re ...

Stop users from inputting dates beyond the current date in Angular 4

Encountering an issue with comparing the date of birth object and today's date object using Moment.js. Even if the entered date is smaller than today's date, it still throws an error. Below is the HTML code: <div class="form-group datepicker ...

Unable to view the token balances of the smart contract on remix while executing the seeBalance function

pragma solidity =0.7.6; pragma abicoder v2; import "https://github.com/Uniswap/v3-periphery/contracts/interfaces/ISwapRouter.sol"; interface IERC20 { function balanceOf(address account) external view returns (uint256); function transfer(address ...

Repeated tweets detected within real-time Twitter stream

I've been working on developing a live update Twitter feature, but I've noticed that it sometimes duplicates tweets and behaves erratically. Did I make a mistake somewhere in my code? http://jsfiddle.net/JmZCE/1/ Thank you in advance (note: I p ...

Best Practices for Variable Initialization in Stencil.js

Having just started working with Stencil, I find myself curious about the best practice for initializing variables. In my assessment, there seem to be three potential approaches: 1) @State() private page: Boolean = true; 2) constructor() { this.p ...

Troubleshooting Issue: XMLHttpRequest Incompatibility with Internet Explorer

I'm having an issue with the script below. It works fine on Firefox and Chrome but doesn't seem to work on IE. I've tried various solutions, including lowering the security settings on my browser, but it still won't work. function se ...

Tips for selecting a random user input interaction in Python

I am looking to create a program that can randomly select a joke from a list of jokes and then allow the user to interact with it, whether it is a knock-knock joke or something else. While I understand how to implement a basic knock-knock joke with user i ...

How to Unpack Intricate JSON Data in C#

Dealing with basic Json object deserialization is not an issue for me. However, I am facing challenges when it comes to nested objects. For instance, consider the following example json that I need to deserialize. { "data": { "A": { ...

Issues with script execution on Ajax tab

I'm currently using JQuery UI tabs to load content through Ajax. I have a situation where two tabs are supposed to load HTML content and execute a script to hide or show certain elements in the loaded content. However, I encountered an issue where the ...

Verify whether an element in the array is being referenced in the file

In an attempt to check if certain array elements are being used, I have the following code. Please correct me where necessary: Firstly, I open the myclass.css file and iterate through each line to add all selectors that start with a hashtag or dot into an ...

Pressing the HTML button will reveal the cart details in a fresh display box

I have been working on setting up a button to display the items in the shopping cart. I have successfully created the cart itself, but now I am facing the challenge of creating a button called showYourCart that will reveal a box containing the cart detai ...

Incapable of stacking one canvas on top of another

I'm new to javascript and looking for help with positioning a canvas element behind another. The code I have may be a bit messy, so any assistance is greatly appreciated. My goal is to have the canvas named "myCanvas" appear behind "coinAnimation". Th ...

Javascript auto submission fails to execute following the completion of the printer script

As someone new to javascript, I have a question. I have a function called sendToQuickPrinter() that prints correctly, but after it finishes executing, I need to automatically submit a form to return to my "cart.php" page. I feel like I'm almost there, ...

What was the reason for the removal of the `encoding` keyword argument from json.loads() function in Python 3.9?

The json package's official documentation explains: json.loads(s, *, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)¶ As of version 3.6: The s parameter now supports bytes or bytear ...