Retrieving information from a JSON object using JavaScript

I am working with an object from a PHP API that looks like this:

[Object { span=1,  caption="Particular",  master=true},
Object { span=5,  caption="Loan Class 1"},
Object { span=5,  caption="Loan Class 2"},
Object { span=5,  caption="Loan Class 3"}]

The desired result should be:

## Particular   Loan Class 1    Loan Class 2  Loan Class 3 ##

I attempted to achieve this using the following code:

var arrData = typeof JSONData != 'object' ? JSON.parse(JSONData) : JSONData;
for (var index in arrData[0]) {
row += index + ',';}
row = row.slice(0, -1);
CSV += row + '\r\n';

This is how the CSV appears:

## span   caption   master ##  

Please advise on extracting the caption values and if there is a script available to export this to excel as merging of columns is necessary.

Answer №1

Make sure to iterate over the entire array, not just the object located at arrData[0]. Avoid using for (index in object), as this only assigns index to the keys and not the values. To access the captions, utilize .caption.

for (var i = 0; i < arrData.length; i++) {
    row += arrData[i].caption + ',';
}

Answer №2

If you're looking to add a caption, you can follow these steps:

var text = arrData.map(function(item) {
  return item.caption;
}).join(' ');

In the above code snippet, we use .map to gather all the caption values from the array and then concatenate them with .join. You have the option to specify the separator when using the .join function.

Converting to an Excel format isn't straightforward. Unless your goal is to output in CSV form (as shown in your example). If that's the case, you may find this solution helpful.

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

A step-by-step guide on deserializing JSON with HTML utilizing Jackson

I have the following JSON data: {"data":"<p><br data-mce-bogus="1"></p>"} Attempting to deserialize this into a Map<String,String> ObjectMapper objectMapper = new ObjectMapper(); objectMapper.readVal ...

Using Three.js to transfer one object's rotation to another object

I have been attempting to transfer one object's rotation to another with no success using the following methods: //The first method rotates much faster than the original object's rotation boxme1.rotateOnAxis(new t.Vector3(0,1,0), cube.rotation.y ...

What is the method for connecting a local JSON Model to a MultiComboBox in SAPUI5?

Is it possible to connect my local JSON model with a MultiComboBox in HTML code? Here is the XML code for the combobox: <MultiComboBox id="multiBox" selectionFinish="onBoxFinish"/> This is how the model appears: var exampleData = { "da ...

Seamless transition of text positioning and opacity as you scroll

After exploring multiple techniques for changing opacity and position on scroll, I am still struggling to connect them effectively. What I'm Looking For: I have a container with 4 words in a row. Only one word should be visible at a time, transition ...

Address a security flaw in the package-lock.json file

After running npm audit on my project, I received the following results: nth-check <2.0.1 Severity: high Inefficient Regular Expression Complexity in nth-check - https://github.com/advisories/GHSA-rp65-9cf3-cjxr fix available via `npm audit fix --force ...

Applying textures seamlessly onto a face of an object in three.js without any breaks

I'm struggling with correctly applying a texture to an object. As shown in this image: https://i.sstatic.net/4WpP4.png, the texture is repeating and not smoothly covering the entire front face of the object. You can access the code and see a live ex ...

How can I swap a string for a symbol in JavaScript?

Is there a way to convert the text @abc some text here to <a href="some_url">@abc</a> some text here using JavaScript? Are there any libraries that can help with this task? ...

ASP.NET WebMethod sends the entire webpage back to JQuery ajax requests

I've been working on a web application that involves calling an ASP.NET WebMethod from jQuery when a textbox is clicked. However, I'm encountering an issue where the WebMethod is returning the entire ASPX Page instead of just the values I need. H ...

adjusting the size of separate models within a unified structure

Recently, I created a 3D model of my hometown and I wanted to incorporate real-time data to adjust the height of the buildings. Initially, I loaded each building as an individual mesh and added it to the scene during setup. var threeObjects = []; var bui ...

Is there a way to replicate the functionality of Python's zip() function in JavaScript

Struggling to translate this function into JavaScript... def pascal(n): row = [1] k = [0] for x in range(max(n,0)): print(row) row=[l+r for l,r in zip(row+k,k+row)] return n>=1 I'm encountering difficulties with th ...

Bootstrap 5.5.2 facing transition issues on bundle.js

Recently, I attempted to incorporate zoom.js into my project to enable image zoom functionality similar to that of the medium website. After linking both the CSS and JS files, it seemed to be working properly. However, I encountered an issue where the tra ...

What is the best way to create a loop for an array that holds objects?

Having an array with multiple objects raises a question. var profile = [ {"MODE":"Comfort","MONDAY":"09:00:00","TUESDAY":"09:00:00","WEDNESDAY":"09:00:00", "THURSDAY":"09:00:00","FRIDAY":"09:00:00","SATURDAY":null,"SUNDAY":null}, {"MODE":"Eco" ...

Issues with date clicking on FullCalendar in Angular

I'm currently using version 4.4.0 of the FullCalendar plugin, but I am facing an issue where the dayClick event is not being triggered in my code. Below is a snippet of my code: calendar.component.html <full-calendar defaultView="dayGridMonth ...

Tips for passing the function name as an argument in Python

I am working with a JSON template called template.py from string import Template test1 = Template(u'''\ { "data": { "name": "$name" } } ''') To generate the JSONs, I have a file named JSONGen.py import ...

Tips for showcasing all values in a nested array

In my Vue application, I am dealing with a nested array where users can select one date and multiple times which are saved as one object. The challenge I am facing now is how to display the selected date as a header (which works fine) and then list all the ...

The Enum class is not providing me with the necessary values

I've developed a specialized class with 2 distinct values. I want to design a manager that can provide a list of these values along with their associated data. Here is my Enum Class: @Getter public enum ModesEnum { ASSOCIATED ("A", "Associated"), ...

Manipulating object information within a nested for loop

I have a variable called jobs in my scope, which is an array containing objects for each department along with their respective data. [ “Accounting”: { “foo” : “foo”, “bar” : "bar" }, “Delivery”: { ...

Text box size in user input not adapting on mobile devices

Website: Visit the Cutter Travel Calculator here I've encountered an issue with my user input text boxes on mobile. Despite setting their width using the formidable forms plugin, the size adjustment doesn't seem to apply when viewed on mobile de ...

Detecting Browser Version Using XML and Javascript

Recently, I created an XML file and attempted to access it using my web browser. It worked perfectly fine in Internet Explorer, but when I tried opening it in other browsers, it failed to function properly. After some investigation, I discovered that the i ...

Using jQuery to substitute a matched word with the each method

Looking for ways to update the text using different methods? Is this correct? It seems like it's not replacing or finding the text accurately. $(function(){ var style_find = ['tab-1','tab-2','rounded-1','rounde ...