Using JavaScript, pass an array containing a list of JSON values

I am attempting to store a list of JSON Objects in an array using JavaScript. Below is the list:

[{"_id":1,"json":{"age":10,"name":"carlos"}},{"_id":2,"json":{"age":10,"name":"carlos"}}]

Here is the code snippet I have written:

var arrayResults = '{"files":' + '[{"_id":1,"json":{"age":10,"name":"carlos"}},{"_id":2,"json":{"age":10,"name":"carlos"}}]}';
alert (arrayResults.files.length);
var jsonData = JSON.parse(arrayResults);

for (var i = 0; i < arrayResults.files.length; i++) {
    var file = jsonData.files.age[i];
    alert(file);
}

Any assistance with this would be greatly appreciated. Thank you!

Answer №1

If you're unsure about what you need, the code below demonstrates how to iterate through your data and display interesting facts about each piece:

var arrayResults = '{"files":' + '[{"_id":1,"json":{"age":10,"name":"carlos"}},{"_id":2,"json":{"age":10,"name":"carlos"}}]}';

var jsonData = JSON.parse(arrayResults);
alert (jsonData.files.length);

for (var i = 0; i < jsonData.files.length; i++) {
    var file = jsonData.files[i];
    alert(file.json.name + " " + file._id + " is " + file.json.age);
}

Your original code had some mistakes, but by studying this response, you should be able to overcome them and make progress with your project.

Answer №2

let dataString = '{"items":' + '[{"_id":1,"content":{"price":50,"name":"John"}},{"_id":2,"content":{"price":20,"name":"Sarah"}}]}';

//console.log(dataString.items.length);
let jsonData = JSON.parse(dataString);

alert(jsonData.items.length);

for (let index = 0; index < jsonData.items.length; index++) {

let item = jsonData.items[index];
alert(item.content.price);
//Counter_name is not defined anywhere in the code
//alert(item.counter_name);
}

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

Steps for updating text within an object in Angular

details = [ { event: "02/01/2019 - [Juan] - D - [Leo]", point: 72 }, { event: "02/01/2019 - [Carlo] - N - [Trish]", point: 92 } ]; I am attempting to modify the text within the titles that contain - N - or - D - The desired outcom ...

Retrieving items from JSON using JQ in a complex nested file without specific identifiers

Trying to extract specific attributes from a generated JSON file obtained through Amazon CLI using JQ. The catch is that the top-level attributes are not named, making it a bit more challenging. [ [ { "State": "running", ...

Retrieving the class name using jQuery

In my HTML code, there is a div with three classes defined as: <div class = "a b c"> .. </div> My goal is to access only the class b using the .attr() method. Is this achievable? ...

Ways to automatically check a checkbox using jQuery

Is there a way to use jQuery to programmatically click a checkbox? I am trying to create a hidden checkbox that will be checked when a user clicks on a specific element, but my current code is not functioning as expected and I am unsure why. You can view ...

What is the best way to reference a const from a different class in React?

I'm relatively new to ReactJS, specifically using NextJS. In my project, I have two files - index.js and welcome.js In index.js, I've added Welcome as a component, along with a const called hideWelcome to hide the component and perform some acti ...

Encountering difficulties in the installation of a package within Next JS

Can anyone assist me with this issue I'm facing while trying to install styled-components on Next JS? I keep getting an error after entering npm install --save styled-components: npm ERR! Unexpected end of JSON input while parsing near '...ry.np ...

Error-free ngClick doesn't trigger AngularJS controller method

I am struggling to trigger the removePlayer(playerId) method upon clicking a button. However, it seems that the method is not being called, as I have placed a console.log() statement at the top and the console remains empty. This situation has left me puz ...

React code is displaying as plain text and the components are not being rendered properly

My latest creation is a component named "componentRepeater" with the main purpose of displaying multiple instances of child components. The props within the "componentRepeater" include properties for the child components, the number of repeats for the chil ...

The image will remain hidden until it is fully loaded and ready to be displayed

I am currently working on a script that involves displaying a loading icon until an image is fully loaded, at which point the loading icon should disappear and the image should be shown. Unfortunately, my current code is not working as intended. Here is a ...

Tips for including a footer element in React:How can a footer division be

I need help with implementing a footer in my React web app. Currently, the body section in index.html looks like this: <body> <div id="root"></div> <script src="index.js"></script> </body> Inside index.js, the rend ...

Adding elements to my state array - Utilizing Nextjs/React/Javascript

After fetching data from 2 different APIs that each return an object, I have stored them in separate states. Now, I want to merge these two objects into a single state that contains both of them as an array. However, when I try to do this, the second obj ...

Is there a way to transfer textbox value to ng-repeat in AngularJS?

1) The Industry dropdown menu corresponds with a code textbox. Depending on the selected industry, the code will change accordingly. 2) There is a dynamic feature to add or delete Movie Names and Directors' names. In this table, there are three colu ...

Single-use binding format

I am a beginner with Angular and I have some doubts about the syntax used in Angular. In AngularJS, we can achieve one-time binding like this: <p>{{::myVar}}</p> In Angular, I know we can do it differently. <p [innerText]="myVar"></ ...

Simultaneously activate the 'onClick' and 'onClientClick' events on an ASP button using JavaScript

I have encountered an ASP button while working on existing code that has both onClick and onClientClick events attached to it. My goal is to trigger both events by generating a click event from an external Javascript file. The line of code I am using for ...

Retrieve the value of a specific inner field in a JSON document by targeting a key with jq

I have a JSON file in the format below. To extract the value of the name field based on the id value, when the id value is 99900, it should return DEF. How can I achieve this using jq? { "11213-99900": { "cid": "11213-99900", "name": "DEF", ...

Modify key-value pairs in a JSON file with the ones from another JSON file using jq (at a different hierarchical level)

I have two documents: file1.json: { "id": "123-456", "name": "John Doe", "email": "johndoe@example.com" } file2.json: { "id": "789-012", "name": "Jane Smith", "email": "janesmith@example.com", "phone": "555-555-5555", "address ...

Javascript promise failing to deliver

As a beginner in the world of JavaScript development, I am excited to be part of the stackoverflow community and have already gained valuable insights from reading various posts. Currently, I am facing an issue where I need to load a file, but due to its ...

Prevent Buttons from Being Clicked While Another is Active in Angular

I am facing a challenge with my Angular functions that enable search features. My goal is to allow only one feature to be selected at a time. /* Trauma Search functionality */ $scope.traumaSearch = false; $scope.traumaText = "Trauma Center"; $scope.togg ...

Error: The function $setTimeout is not recognized

Currently enrolled in a course and facing an issue while developing a Tinder-like app for ProductHunt using Ionic and Angular. ionic $ 0 776918 error ReferenceError: $setTimeout is not defined at Scope.$scope.sendFeedback (http://localhost:81 ...

Creating a New Line in JSON Format Using Flask Python and Displaying it as HTML

I've been struggling to properly format JSON with new lines, but all my attempts have failed. from flask import * import json app = Flask(__name__) @app.route("/user/", methods=["GET"]) def user(): datajson = {"Author&q ...