What is the best way to transfer selected rows from table A to table B while adhering to specific conditions?

I need to generate a specific tab based on the data in my JSON response. Here is what I require:

markers: [
      position: {
         lat:50.8999208,
          lng:20.6258000
      },
      vin: vinfromresponse
    },
    {
      position: {
         lat:50.8911111,
          lng:20.6259999
      },
      vin: vinfromresponse
    },
]

The JSON response from my REST API is structured like this (although typically there are multiple objects rather than just one):

[{"id":1,"vin":58222,"register_number":"TK2332","cost":1.32,"latitude":20.6285908,"longitude":50.872941,"service":false,"reservation":false}]

However, the new array I want to create must have field names 'lat' and 'lng' for it to work with Google Maps API markers. I am unsure how to properly assign the data from my JSON response to fit this structure. I lack experience with JavaScript and could use some guidance.

Answer №1

Check out the .map method on MDN

Enjoy exploring! :P

let data =[{"id":1,"vin":58222,"register_number":"TK2332","cost":1.32,"latitude":20.6285908,"longitude":50.872941,"service":false,"reservation":false}]

let object ={ }
object.markers = data.map(item=>{
     return { 
       position: {
         lat:item.latitude,
         lng: item.longitude
      },
      vin: item.vin}
  }
)
console.log(object)

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 Google Map information does not show up after I expand the accordion panel

Whenever I expand the navigation panel on my webpage, the Google map doesn't display anything and just appears as grey. It's only when I open the console that the map starts to appear. What changes do I need to make in my code so that the map sho ...

Error encountered when attempting to access API using the provided key

My current challenge involves integrating my Django application with the API developed by my team. I have crafted a script that constructs the required string and then sends a request to the API. My intention is to fetch the results back to a view for disp ...

What is the function of computeCentroids in Three.js?

While working on my custom geometry in three.js with TypeScript, I encountered an issue where the object appeared dark with Lambert material. To troubleshoot, I examined the source code and noticed that nearly every geometry class had the following two lin ...

Utilizing moment.js to showcase the current time of day

Is there a way to show "Sunday Morning" if it's before noon or "Sunday Afternoon" if it's after noon? This code snippet below is what I am currently using to retrieve the current day: var now = moment().format("dddd"); $("#date").append(now); & ...

Attempting to modify PHP query following submission of data via Axios in Vue application

Fetching a JSON object through a PHP query from a service known as BullHorn can be done like this: <?php echo getBhQuery('search','JobOrder','isOpen:true','id,title,categories,dateAdded,externalCategoryID,employmentTy ...

How can we ensure that the element being hovered over is brought to the forefront and placed in the center?

I have been tasked with replicating this specific object for a project. The creation process is going smoothly, but now the requirement is to make it stop when the user hovers over it. Implementing this hover functionality is not a problem as CSS provides ...

The issue of integrating jQuery with the textarea CKEditor is still unresolved

I'm having an issue with applying the ckeditor function to append data in my code. I have included a cdn link for ckeditor in the header.php file, but it's not working as expected. How can I resolve this? <script> $(document).ready(func ...

Tips for modifying JSON response using a function

When I call the function buildFileTree, I store its response in a constant variable called data. const data = this.buildFileTree(dataObject, 0); The value of dataObject is: const dataObject = JSON.parse(TREE_DATA); And the content of TREE_DATA is: cons ...

What steps can I take to ensure a mySQL transaction is not executed simultaneously in a Node.js Express application?

I'm facing a challenge that seems quite simple, yet it's causing me a lot of frustration. I just need to ensure that a MySQL database transaction is mutually exclusive, meaning a second call should wait until the first one finishes. Despite tryin ...

.slideDown Not Functioning Properly on my System

After successfully linking my index.html file to jQuery, I am facing an issue with the .slideDown code. I'm not sure if there is a problem with the code itself or if I didn't attach jQuery correctly. Can anyone help me troubleshoot this? Below i ...

Exploring AngularJS for real-time updates from a persistent database

Hello, I'm new to Angular and currently working on an app that requires frequent polling of a URL every second. The retrieved data needs to be stored persistently for access across multiple views and controllers. To handle this, I've placed the ...

My root route is being loaded by jQuery, which also triggers the loading of all the scripts again using X

After struggling with this issue for a few days, I've decided it's time to seek some help. The problem I'm facing is during the update of my Angular app from v1.3.0-beta.17 to v.1.3.x (head). I keep receiving a message saying WARNING: Tried ...

Utilize a vacant implementation of an interface

I have a simple interface called EmployerContact. I'm wondering how I can create an empty object, mockEmployerContact, of type EmployerContact and then assign values to its properties later on. Do I need to start with default values for this object? e ...

Tips for efficiently serving a static file without triggering a disk read

res.sendFile is the preferred method for serving a static file in express. However, it appears that res.sendFile reads the file from disk with each request, as shown below: router.get('/', (req, res) => { res.sendFile('./guest.js&apo ...

Using the $.ajax function with the PUT method and sending an OPTIONS request

Here is my code snippet: function test(segmentId) { var url = "http://...../api/avoidsegments/123456"; $.ajax({ url: url, type: "PUT", contentType: "application/json", data : { "appID": ig_appID, ...

JavaScript functions with similar parent names

Explain a function that has identical functionality to its parent parent.document.getElementById(source).innerHTML should be the same as other-function-name.document.getElementById(source).innerHTML ...

Storing extensive JSON data in PostgreSQL using Django is an efficient way to manage

In my database, I have a model called Test: class Test(models.Model): title = models.CharField(max_length=32, verbose_name='title', default='') json = models.JSONField(default=dict) ... My application frequently receives da ...

MS Edge modifies the attribute's empty value to 1

I have written a JavaScript code to extract values from a list, but in the Windows Edge browser, it returns a value of 1 even when the actual value of the <li> tag is blank. For example: HTML Code <ul> <li value="">Test 1</li&g ...

How can I iterate over a list of views in PostgreSQL using a for loop?

I am looking to create a for loop in PostgreSQL, where I can perform insert into from select. The target table remains constant, but the source views vary. My goal is to iterate through a list of similar structured views and execute the insertions. To pro ...

What is the best way to retrieve the size of a JSON object that is stored in the local project directory

I am working on a spring-boot project with the following file structure... -src -target -pom.xml -uploaded_json -- example.json Here is the content of my example.json file: [ { "Username": "U1", "Password" ...