Getting rid of quotes in a JSON result

My unique code snippet

Retrieve data = Array[2] : 
                 0:object 
                     id : "1"  lat : "76.23" long:"21.92" 
                 1:object 
                     id:"2" lat:"10.23" long:"12.92"

var newCoords=[];

for(_i = 0; _i < retrieve_data.length; _i++)
{

     var tmp = retrieve_data[_i];
     var coordinates = [retrieve_data[_i]['lat'], retrieve_data[_i]['long']];
     newCoords[tmp.id] = JSON.stringify(coordinates);
 }
 console.log(newCoords);

The result stored in newCoords is -

 [1: "["76.2350","21.9253"]", 3: "["10.5650","21.3653"]", 6: "["60.5650","55.3653"]"]

this represents a JSON string

However, I am looking to format it as follows - {"1": [76.235,21.9253], "3": [10.565,21.3653], "6": [60.565,55.3653]}

Is there a solution to achieve this formatting?

Answer №1

Utilize the parseFloat method to convert strings to numbers,

var abc = new Object();
for(_i = 0; _i < swipe_data.length; _i++){
    var t1 = [parseFloat(swipe_data[_i]['latitude']), parseFloat(swipe_data[_i]['longitude'])];
    abc[tmp.id] = t1
}
console.log(JSON.stringify(abc));

Give this code a try, it should function correctly.

Please keep in mind:

  1. abc needs to be an Object, not an Array.
  2. Avoid using JSON.stringify when assigning values to keys.
  3. Only use JSON.stringify once all key values have been updated in your Object.

Answer №2

The perfect solution to this problem is as follows:

let newArray = [];

for(let i = 0; i < data.length; i++)
{
    let temp = data[i];
    let coordinates = [data[i]['latitude'], data[i]['longitude']];
    newArray[temp.id] = JSON.stringify(coordinates);
    newArray[temp.id] = newArray[temp.id].replace('"','');
}
console.log(newArray);

This code snippet effectively removes any quotation marks found in the string.

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

The request for an advertisement was successful, however, no ad could be displayed as there was insufficient ad inventory available. Please handle the situation appropriately with the

Using react-native, I am trying to incorporate ads into my app but encountering an error. Despite attempting various solutions, nothing seems to work. It appears that the issue may lie with the AdMob Android SDK. While I have reviewed SDK videos related to ...

Order of execution for setImmediate() and setTimeout() callbacks compared to I/O callbacks

In the world of Node.js, the event loop powered by libuv is divided into specific phases. The poll phase is where we wait for I/O tasks to complete before running their associated callbacks. The length of this waiting period is determined by timers, timeou ...

When extracting the host URL and destination URL

When making an Ajax POST call to retrieve data from a file on a different server, I am encountering an issue where my host URL is being added to the destination URL for my AJAX request. Host URL: 192.168.1.2 Destination URL: 192.168.1.7/var/www/html/pfe/ ...

Iterating through an array and displaying information according to the quantity in node.js

Hey everyone, I have a simple task at hand where I am working with the following array: {items:[{upc:a,quantity:2},{upc:b,quantity:3}]} My goal is to transform it into the format shown below: {products:[{barcode:a},{barcode:a},{barcode:b},{barcode:b},{bar ...

Troubleshoot: Firebase deployment of Vue.js application unable to locate page

I'm currently working on a silly web app with my friends for some laughs. We're using Firebase hosting and Discord OAuth2. The issue arises when attempting to access the "/login" page either by entering it in the URL or clicking "authorize" on th ...

Issue with iOS iframe scrolling - unable to scroll as expected

On iOS devices like the iPad and iPhone 6, scrolling doesn't seem to work as intended. Instead of the iframe scrolling, it's the 'body' that is moving. Javascript $(document).on(clickHandler, '#content a', function(){ href ...

Animating nested ng-repeat loops

Trying to animate a list of elements divided by rows (the parent ng-repeat) and columns (the child ng-repeat) has been quite challenging. While I was able to achieve the desired animation with individual ng-repeats, the same cannot be said for nested ng- ...

Creating a customized function in javascript/jquery with the ability to override it

Currently, I am utilizing Visual Studio for writing JavaScript/jQuery. For instance, when I input: $('#selector').text('foo') and then highlight text and hit F12, Visual Studio directs me to the file jquery-2.2.3.intellisense.js, auto ...

Set a restriction on the Bootstrap DatePicker to only show dates within a

My application features StartDate and EndDate datepickers, and I need to implement a 30-day limit on the selection range to prevent performance issues caused by loading too much data. I'm looking for a functionality where if the user picks today as t ...

Produce a vue attribute

I am looking to display a property in my component. My template includes: <v-flex v-for="c in components"> <component :is="c.component" v-bind="c.prop"></component> </v-flex> And in the script: ... mounted(){ ...

Unable to adjust the padding of a div while the Bootstrap 4 navbar is in a collapsed state on mobile devices

I am facing an issue with a fixed navbar at the top of my page. Below the navbar, I have the page content which includes a Bootstrap 4 image carousel. The problem arises on mobile devices where I need to add top-padding to the page-container (below the nav ...

Mastering Vue 3: Simplifying a reactive array of objects while maintaining reactivity

Struggling with maintaining reactivity in Vue 3 when flattening a nested array of objects. Unfortunately, my attempts result in crashing and browser hang-ups. In my Vue 3 component, I have an array structured as a list of objects: this.grouped = [ ...

Tips for utilizing the onclick method to modify an ejs variable

, I am currently in the process of developing a web application utilizing Node, Express, and ejs. Specifically, I have created a page that features a series of buttons (displayed in the first table) alongside an associated set of hidden divs (shown in the ...

What is the best way to switch a Boolean value in React Native?

Struggling with toggling a Boolean state from true to false when the result is undefined. Tried several methods but none seem to work. The boolean state in the constructor is defined like this: class UserInfo extends Component{ constructor(props){ s ...

Tips for integrating TypeScript with Vue.js and Single File Components

After extensive searching online, I have struggled to find a straightforward and up-to-date example of setting up Vue.js with TypeScript. The typical tutorials out there either are outdated or rely on specific configurations that don't apply universal ...

Unexpected issue with sliding menu toggle implementation

I have been struggling to make a menu slide to the left when a button is clicked, and then slide back to its original position when the same button is clicked again. Although I have been using the toggle method, it doesn't seem to be working for me. I ...

Ways to retrieve the user's IP address and provide the information in JSON format

Although I am not an expert in PHP, I specialize in developing Android apps. One of the challenges I face is extracting the user's IP address from a specific URL . This URL provides various information when accessed, but my main requirement is to retr ...

Can you explain how to add a string array to an HTML div using AJAX?

My output consists of a string array that comes from PHP JSON. I am trying to display this output in an HTML div tabcontent, but I'm unsure of how to achieve this. Below is a snippet of my code - can anyone help me with this? [{"id":"1","p_name ...

Angular protection filter

Every time I used Angular CLI to generate a new component, it would create the component with standard parameters: @Component({ selector: 'app-test1', templateUrl: './test1.component.html', styleUrls: ['./test1.component.css ...

Switching the cursor to the following input after a paste event using JQuery

I am currently working on an HTML form that consists of multiple input boxes. I'm looking to focus on the next input box after pasting content into one of them. Below is the code snippet I have been using: $("input").bind('paste', function( ...