Send the JSON data as separate objects rather than combining them into a single array

I'm currently working on pushing data from an API into an array of objects.

This is what I want:

myFXdata {[ccypair], [resistance], [support], [trend.src]},
         {[ccypair], [resistance], [support], [trend.src]},
         {[ccypair], [resistance], [support], [trend.src]},
         {[ccypair], [resistance], ...etc},

However, this is the result I get:

myFXdata {[ccypair], [resistance], [support], [trend.src],
         [ccypair], [resistance], [support], [trend.src],
         [ccypair], [resistance], [support], [trend.src],
         [ccypair], [resistance], ...etc}

This is my code:

 var myFXdata = [];
 
 for (var i = 0; i < collection.length; i++){
    
    myFXData.push((collection[i].ccyPair),   (collection[i].resistance), (collection[i].support), (collection[i].trend.src));  
         }

    console.log(dailyfxTech)

I believe I need to push into a new object each time, but when I try:

     myFXData.push({collection[i].ccyPair});

I encounter this error:

SyntaxError: Unexpected token '['. Expected a ':' following the property name 'collection'.

Any suggestions?

Answer №1

Your desired output is not achievable as it does not represent a valid JavaScript object:

{[ccypair], [resistance], [support], [trend.src]}

Based on your snippet, it seems like you want an array of objects, each containing 4 properties without keys, which are also arrays themselves. This structure doesn't align with the requirements for objects in JavaScript.

A corrected version of your code snippet could be:

{'pair': ccypair, 'resistance': resistance, 'support': support, 'src': trend.src}

If using an array works better for your scenario, you can consider:

[ccypair, resistance, support, trend.src]

Depending on your chosen format, you can easily append these values to your array:

myFXData.push({
    pair: collection[i].ccyPair, 
    collection: collection[i].resistance, 
    support: collection[i].support, 
    src: collection[i].trend.src
});

or

myFXData.push([
    collection[i].ccyPair, 
    collection[i].resistance, 
    collection[i].support, 
    collection[i].trend.src
]);  

The first option results in an array of objects, while the second option generates an array of arrays (a two-dimensional array). I hope this explanation clarifies things for you. Feel free to ask if you have any further questions.

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

display Ajax Response in dropdown selection

My task involves selecting multiple tests and dates. Upon clicking submit, the names of laboratories associated with the selected tests are loaded into a select option. Using Ajax script: $('[name=submits]').click(function(e) { e.preventDef ...

Dynamically divide canvas screens based on fabricjs dropdown selection

I am attempting to implement split screens in fabric js, such as 1, 2, 4, 8, and 16. The screen should split based on the selection from the dropdown menu. Check out my current code where I have successfully uploaded images. If I click on the images, th ...

What is the solution to the question: "Hey there, time traveler. We are currently in an era where CSS prefixes are no longer necessary, thanks to the advances in prefix-less CSS

I'm having an issue in my Next.JS app with importing my stylesheet in _app.js. Here is how I currently import it: import '../public/css/Index.css'; The content of Index.css looks like this: .index-container { margin: 20px auto 0; } Wha ...

I'm having an issue where whenever I click on a different page, I keep getting redirected back to the first page

Upon conducting research, I discovered that by implementing the refined code below, I was able to resolve my issue (my other html was also corrected using this solution) setTimeout(function() { datatable_FTP.ajax.reload(null, false); }, 30000); Although I ...

Transmitting MySQL blob data in JSON format for the response

I'm currently working on a GET method that retrieves a photo stored in a blob type field in MySql and returns it. My goal is to send the photo back to the client within a JSON string format so it can be displayed in an AngularJS application. def GET( ...

"Enhancing HTML tables with jQuery for a polished look

This table belongs to me: <tr class=items> <td id=id_1></td> <td id=city_id_1></td> <td id=temp_1></td> <td id=date_1></td> </tr> ...

When the responsive navbar is activated, it obscures the body content, causing a block in

When attempting to create a solution, ensure that the responsive toolbar is enabled on your Chrome browser. My approach involves designing for mobile first and then adapting for desktop. However, I am encountering an issue where the hamburger menu opens an ...

What is the optimal method for assigning a value to a specific key within a JavaScript JSON object?

Below is the information stored in a file called "fokontanys.json": { "vzdveg643": { "lldistrict":"Ambilobe", "id_province": 7, "id": null }, "vzvsdv5327": { "lldistrict ...

Leveraging geoPosition.js in conjunction with colobox

How can I create a colorbox link that prompts the user for permission to access their location, and if granted, displays a map with directions from their current location? I've managed to get it partially working, but there's an issue when the us ...

Having trouble getting req.files to work in a Node.js Express application?

Hello there, I'm facing an issue with accepting an uploaded file. Every time I call req.files, it comes out as undefined. I can't seem to figure out what I am doing wrong... Below is a snippet of my app.js file: var express = require('expr ...

"Bringing together ECMA harmony: a nod to callbacks and generators

Initially, we are exploring uncharted territory here. Although it functions in the latest versions of Firefox, the documentation on MDN is not yet ready at the time of writing. I will address the MDN later on (maybe, as there are numerous areas needing att ...

JavaScript Implementation of Min Heap

Hello! I've been working on implementing a min heap in JavaScript and I have a question about the removeMin algorithm. I am using an array to store the heap internally. When percolating downwards, I am currently using the condition 2 * k <= this.si ...

Consolidate code by implementing on selectmenu

I need assistance with handling multiple select menus on a View page Below is a snippet of the code: $(function() { var selectSpeed = $('#speed'), selectTest = $('#test'); selectSpeed.selectmenu(); selectTest.selectmenu() ...

I'm stumped trying to understand why I keep getting this syntax error. Any thoughts on how to fix it?

Our team is currently working on creating a dynamic SELECT box with autocomplete functionality, inspired by the Standard Select found at this link: We encountered an issue where the SELECT box was not populating as expected. After further investigation, ...

Excel VBA user-defined function - Struggling to successfully transfer a variant array generated by one UDF as an argument in another UDF

I'm having trouble using the array generated by a UDF I created as an argument for another UDF. The function is returning a #value error. I can't seem to identify where the issue is. Here's the code snippet: Any assistance or advice woul ...

The firebase Function did not return a valid response, it was expecting a Promise or

Hello, I have been working on some code to send friend request notifications from one Android app to another. Unfortunately, I am encountering an error that says "Function returned undefined, expected Promise or value" in the functions console. Additionall ...

Exploring ways to access JsonResponse dictionary using jQuery in Django

AJAX Request $('#id_buysell').on('change', function(){ console.log("buysell"); var $id_buysell = $('#id_buysell').val(); console.log($id_buysell); $.ajax({ me ...

The eBay inventory API is displaying listings as "Out of Stock" despite the fact that there is actually stock available

I have been using the eBay Inventory API to effectively manage our eBay inventory. Recently, I made some modifications to existing API functions that were not originally written by me. Unfortunately, due to a confirmed bug on eBay's end (which has sin ...

Generate a dynamic form that automatically populates different input fields based on JSON data

I am trying to dynamically auto populate a form with various input types such as select boxes and text areas. I have successfully implemented this for input boxes, see example below: function autofill(){ var data = [{visible_retail: "0", brand: ...

Experience the impact of a lagging network connection on Chrome extensions through the power of React-NextJS

I'm currently working on a Chrome extension that requires me to limit download speeds in the browser programmatically (client-side). Despite researching extensively on the subject, I have not been able to find any information on how to achieve this us ...