fetching data from a JSON object in an array with JavaScript

As a novice, I need assistance in extracting values from the following array json object.

[{"Category":"HI","Sub Category":"AQ HIOP"},
{"Category":"2HJ","Sub Category":"AS HIOP"},
{"Category":"3HJ","Sub Category":"AT HIOP"},
{"Category":"4Hj","Sub Category":"AP HIOP"},
{"Category":"5HJ","Sub Category":"AN HIOP"},
]

My requirement is to access only the values of Sub Category.

Thank you for your help in advance.

Answer №1

Give this a shot:

var items = [{"Product":"Apple","Type":"Fruit"},
    {"Product":"Milk","Type":"Dairy"},
    {"Product":"Bread","Type":"Grain"},
    {"Product":"Chicken","Type":"Meat"},
    {"Product":"Carrot","Type":"Vegetable"}
    ]
    for (var i = 0; i < items.length; i++) {
    for (property in items[i]) {
    console.log('Property: '+ property + ' Value: ' + items[i][property]);
    }
    }

You can utilize for loops and for in loops to access your object properties.

Answer №2

There are several methods to achieve this. If your Object is stored in a variable, you can access it in the following way:

var test = [{"Category":"HI","Sub Category":"AQ HIOP"},
{"Category":"2HJ","Sub Category":"AS HIOP"},
{"Category":"3HJ","Sub Category":"AT HIOP"},
{"Category":"4Hj","Sub Category":"AP HIOP"},
{"Category":"5HJ","Sub Category":"AN HIOP"},
]

test[0]["Sub Category"] //Note that the object is within an array

If the key does not have any spaces, you can access it like this:

test[0].Category;

If your JSON is in string format, you need to use JSON.parse first.

Edit

Here is a guide on how to insert into a new array:

var another_array = []
another_array.push({"New category": test[0]["Sub Category"]})

Answer №3

let data = [
{"Category":"XZ","Sub Category":"LK XZOP"},
{"Category":"YZ","Sub Category":"LS XZOP"},
{"Category":"Z1","Sub Category":"LT XZOP"},
{"Category":"Z2","Sub Category":"LP XZOP"},
{"Category":"Z3","Sub Category":"LN XZOP"},
];
data.forEach(function(entry){
    console.log(entry);
})

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

What is the best way to incorporate the :after pseudo-element in JavaScript code

HTML: This is my current code snippet <div class="one"> Apple </div> I am looking to dynamically add the word "juice" using JavaScript with the .style property. Is there an equivalent of :: after in CSS, where I can set the content in JavaS ...

Loading the JS file after waiting on Lib (IronRouter) causes the HTML not to load properly

Currently, I am utilizing wait-on-lib along with IRLibLoader.load() in conjunction with Iron-Router, following the instructions provided in the tutorial found at: . My objective is to load external javascript code. Below is a snippet of my routing code: R ...

What's the issue with this code not functioning correctly?

puts.php (JSON) { "image":[ { "name":'<div id="yes">Hi!</div>' } ] } process.php <HTML> <HEAD> <TITLE>Process</TITLE> <script type="text/javascript" s ...

Tips for creating a stylish ReactJs Header component that stays fixed at the top of the page

Having an issue with my Header component - I want it to remain fixed at the top while scrolling down. Here's the current code: I've attempted using "position = fixed", but it caused my header to resize. Then, I tried setting its width to "width ...

Is there a more efficient method for performing multiple JavaScript replace calls rather than executing them individually?

In my Javascript code, I am creating a string that includes HTML content. Currently, my approach involves the following steps: var filter = ""; filter = util.getTemplate( "tmp_filter", temps ); filter = filter.replace( 'id="tmp_filter"','& ...

The request.body in Express.js is currently undefined

const express = require('express'); const cors = require('cors'); const app = express(); app.use(express.json()) app.use(cors()); app.post('/', (req,res) => { console.log(req.body); res.send('received') ...

Isotope animation glitches causing malfunction

I've been experimenting with CSS3 transitions on isotope items, but I'm encountering some strange behavior. My goal is to achieve a fading effect on the items, similar to this example: . Thank you in advance! Here's what I have so far: http ...

Transforming Excel data into JSON format using ReactJS

Currently, I am in the process of converting an imported Excel file to JSON within ReactJS. While attempting to achieve this task, I have encountered some challenges using the npm XLSX package to convert the Excel data into the required JSON format. Any as ...

Locating items based on checkboxes and dropdown selection

My goal is to calculate the sum of certain numbers based on checkboxes and a select option. Below is the code I am using: <div class="container"> <select> <option value="1">1</option> <option value="2">2</option> <o ...

Configuring Vuex State

Currently, I have a prop that is being bound to a child component. My goal is to eliminate this binding and instead assign the value to a data property in a vuex global state. <VideoPip :asset="asset" v-show="pipEnabled === true" /&g ...

Exploring a React JS option for filtering repeated elements, as an alternative to Angular

While using Angular JS, I came across the 'filter' option within ng-repeat which is useful for live search. Here's an example: <div ng-repeat="std in stdData | filter:search"> <!-- Std Items go here. --> </div> &l ...

How to Process a Stripe Payment using jQuery AJAX (JavaScript Only)

I'm in the process of creating a custom payment form for Stripe and I want to manually initiate the AJAX call to connect with Stripe. Instead of relying on a typical submit event. Unfortunately, it seems like I might be sending the request to the inc ...

In Vue3, have you ever wondered why the $emit function seems to work fine before a promise fetch,

https://i.sstatic.net/yJmDY.jpg I have encountered an issue while attempting to pass the result of a promise fetch from a child component to a parent component using emit. Strangely, the emit function was working perfectly fine before the $fetch operation, ...

Unable to supersede CSS module styling using global or external stylesheets in React, Next.js, or React Native

I'm facing a challenge finding a solution to a basic issue. I have a component that is initially styled using a class from a *.module.css file. However, I want to replace this style with one from a global stylesheet or another stylesheet but I can&apo ...

Obtain the selected dropdown value and transfer it to the controller seamlessly without the need to reload the page

Currently, I am facing an issue with two dropdown lists in a bootstrap modal - CATEGORY and SUBCATEGORY. The values in the SUBCATEGORY list depend on the selection made in the CATEGORY list. My goal is to retrieve the selected value ID and pass it to my co ...

Error Encountered During Building Apache Cordova Project in Visual Studio 2015

Encountering an issue when attempting to launch my cordova project on both an android device and android emulators. Currently utilizing visual studio 2015 In dire need of assistance! Error can be viewed in the image below: ...

Using Spring's jsonview to extract a parameter from a request

I am trying to pass a request parameter to a method that returns a property of JSON. How can I achieve something like this: { "project":{ "id": 1, "cashflow":[{ date: "2014-09-27T18:30:49-0300", value:-1000, ...

Having trouble uploading an image using the Cloudinary API in a Next.js project

I am currently working on a page meant for creating posts. Initially, the page was designed to handle multiple image uploads. However, I made some adjustments so that it can now accept only a single image upload. Unfortunately, after this modification, the ...

Retrieve JSON data without keys in Python

I need assistance with extracting location data from a JSON API service. Here is my current progress: >>> import json >>> import urllib >>> from urllib import urlopen >>> url = urlopen('THE API URL').read() &g ...

The issue with Nextjs getStaticPaths is that it is not retrieving data from Firebase Firestore

I'm encountering an issue with fetching dynamic data from firestore in nextjs using getStaticPaths. While rendering the data from firestore with getStaticProps works, I'm facing a problem when trying to access specific item details as it leads me ...