Working with JSON data and utilizing a straightforward 'for' loop

If I receive an array of data

var information = {"response":"OK","details":[["Marco","123"],["John","44245"],["Wayne","645464"]]}

How do I loop through them and log the corresponding values to the console?

Marco 123
John 44245, ..

Marco - 123
John - 44245, ...
for (var i = 0; i < info.length; i++) {

}  

Answer №1

data.data.forEach(element => {
    console.log(`${element[0]} - ${element[1]}`);
});

Answer №2

for (let j = 0; j < dataArray.items.length; j++) {
   console.log(dataArray.items[j]);
}

This seems straightforward, but is there another aspect of the problem I'm overlooking?

Update:

It turns out it was deceivingly simple...

To confirm the accuracy of my answer, to extract only the values from the data within dataArray, the correct approach is:

const dataArray = {"result":"Success","items":[["Sarah","333"],["Mike","789"],"Eva","456"]]};
for (let j = 0; j < dataArray.items.length; j++) {
   console.log(dataArray.items[j][1]);
}

However, @user987654 offered a more efficient solution.

Answer №3

When working with a data object, the key to accessing the actual data is through data.data. This will give you an array of values that you can then iterate through to extract each individual value.

var data =  {"status":"Success",
        "data":[["Alice","456"],["Bob","7890"],["Eve","12345"]]
        }

var newData = data.data; //the data key holds an array

newData.forEach(function(item){
      item.forEach(function(innerItem){
       console.log(innerItem)
   })
})

JSFIDDLE

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

Error: ReferenceError: The object 'angular' is not defined in the script.js file at line

I have gone through all possible solutions but none of them have helped me resolve the issue. I have a simple code that fails to execute due to the error mentioned. Here is my code index.html <!DOCTYPE html> <html data-ng-app="piApp"> & ...

Vue2 is not compatible with the vue-email-editor component

I checked out the official website to install the vue-email-editor. Here is the link for the unlayer vue-email-editor component However, I encountered the following error: vue.runtime.esm.js?c320:4573 [Vue warn]: Error in render: "TypeError: (0 , ...

Tips for accessing an Angular service from different Angular controllers

I am a beginner with angular js and I am currently exploring ways to call the service provided in the code snippet below from a controller. The service is defined as follows. app.factory('myappFactory', ['$http', function($http) { v ...

Fill the number array by selecting options from a dropdown menu

Despite my efforts, I have been unable to successfully populate a dropdown list with an array. Some resources I've attempted to use include: A related question on Stack Overflow: JavaScript - populate drop down list with array This example code sni ...

Slider malfunctioning following AJAX loading

After the user clicks on #mail-wrap, I am attempting to trigger the ready event which loads another page with AJAX so that sss() can be refired. Despite my efforts, it seems like it is not working as expected. Can you help me identify what might be going w ...

Use Javascript to deactivate the mouse cursor and rely solely on the keyboard cursor for navigation

I am facing an issue with a div that contains a textarea. The cursor is automatically positioned at the beginning of the text within the textarea. I would like to disable the mouse cursor when hovering over the textarea but still be able to navigate within ...

Tips for choosing every checkbox in React?

I am working on a module that imports various components: import React, { Component } from 'react' import EmailListItem from './EmailListItem' import { createContainer } from 'meteor/react-meteor-data' import { Emails } from & ...

I am looking to dynamically populate one dropdown menu based on the selection made in another dropdown menu by utilizing jQuery along with a JSON

Below are my selections along with a JSON array that could be utilized to populate the second div based on the choice made. To begin, I need to clear the options in the second select and then proceed to populate them while setting the selected value as th ...

I need help crafting a regular expression that specifically prohibits the use of semicolons, colons, single quotes, and

I am attempting to create a regular expression that does not allow semi-colons, colons, single quotes, and double quotes. var address=/^[^\u0022\u0027\u003A\u003B]{1,50}$/ address.test(value); This is my code. This code will only ru ...

Is it possible to utilize the everyone role in discord.js?

I'm currently working on integrating my lock and mute command with a per-server settings feature that I recently implemented using mongodb. My goal is to have the command retrieve the member's role from the database (roles.cache.get(guildProfile. ...

Ways to ensure a video completely fills the frame

I am experiencing an issue where the video in the main div of my home page does not fully fill the div right away - I have to refresh the page for this to happen. As a result, there is black space on the left and right sides. Interestingly enough, this pr ...

The jqGrid fails to display JSON data in the grid properly

Despite the fact that my action method is returning data in JSON format, the jqGrid control seems to have trouble rendering it. Below is the code for the method that returns the data in JSON format: ContactContext db = new ContactContext(); // / ...

Provide MongoDB ID for SQL Server across several entries

I am facing an issue with my data migrator tool that is moving data from SQL Server to Mongo. The problem occurs when trying to update the SQL table by inserting the generated ID from Mongo into it. An error message "RequestError: Requests can only be made ...

Utilize Jq in Shell script to analyze and identify differing values between two JSON files and display the unmatched data

Is there a way to extract the objects from one JSON file that do not have a match in another JSON file? For example, JSON file1: [ { "name": "ABC", "age": "23", "address": "xyz" }, { &qu ...

Import MDX metadata in Next.js on the fly

I am currently utilizing Next.js to create a static blog site. Following the guidelines in Next.js documentation, I set up @next/mdx and successfully imported MDX statically using import MDXArticle from "@/app/(article)/2023/test-article/page.mdx&quo ...

Why isn't my JSON data being transmitted accurately from the front end to the back end?

Below is the JSON data that I am working with: { "fields":[ {"name":"thom","techname":"rgom","description":"dfgkjd","type":"text"}, {"name":"thom","techname":"rgom2","description":"dfgkjd","type":"text"} ] } Upon posting this JSON to a NodeJ ...

Fetching JSON data on an Android platform

Hi there, I'm new to the world of Android development and I'm facing some trouble retrieving data from the JSON code provided below. If you have any suggestions or tips, please share them with me. Here is the JSON Code: {"category":{"name":["Sp ...

Which grid system is most effective for use in Angular applications: ui-Grid, ag-Grid, or another option altogether?

What is the top-performing Angular grid for MVC API Applications that offers excellent functionality for clients? ...

What is the best way to import an array of integers into a Pig Script using JsonLoader?

I am facing a challenge with reading a JSON file that contains the following line: "helpful": [ 5, 5 ] Is there a way to parse this in a pig script without relying on external libraries like elephant-bird, as suggested here? Although closed tick ...

How can we programmatically add click handlers in Vue.js?

Currently attempting to add some computed methods to an element based on mobile viewports exclusively. Here is a simplified version of my current project: <a class="nav-link float-left p-x-y-16" v-bind:class={active:isCurrentTopicId(t.id)} @click="onTo ...