Obtaining JSON data in React Native without prior knowledge of the key

When I receive this type of data, the keys are common but the items vary. How can I extract and add this data to my list of categories?

{
   "99": "Venues",
   "100": "Party Supplies",
   "101": "Entertainment",
   "102": "Desserts",
   "103": "Catering"
}

This is how I attempted to populate my list:

const dataItem = data.data;
const newList = [];
newList.push({item: dataItem[99]});
newList.push({item: dataItem[100]});
newList.push({item: dataItem[101]});
setList(newList);

But what if the keys are not consistently named? For example:

{
    "S99": "Venues",
    "SDF100": "Party Supplies",
    "CF101": "Entertainment",
    "VF102": "Desserts",
    "CFCV103": "Catering"
}

How can I add this data to my list without knowing the exact key format?

Answer №1

To retrieve both key and value, use the code snippet below:

for (var item in info.data) {
   console.log(item); // This will display the key, which could be S99, SDF100, etc.
   console.log(JSON_Object[item]); // This will show the corresponding value for the key.
   updatedList.push({element: info.data[item]});
}

This process allows you to populate your list with additional values.

Answer №2

const items = {
 "S99": "Venues",
 "SDF100": "Party Supplies",
 "CF101": "Entertainment",
 "VF102": "Desserts",
 "CFCV103": "Catering"
}

const itemList = Object.values(items);
console.log(itemList);

// displays (5) ['Venues', 'Party Supplies', 'Entertainment', 'Desserts', 'Catering']

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

Convert a DateTime object to JSON using the Mongo C# driver's toJson

I have a dataset stored in MongoDB that looks like this: "trd" : ISODate("2003-12-08T00:00:00Z") Currently, I am retrieving data from Mongo as BsonDocument using the following approach: var builder = Builders<BsonDocument>.Filter; var ...

validation using ajax and php

One question I have is, when I include document.getElementById('myPassword').value); in the validarDados function, it corresponds to valor. Then I need another variable, but valor1 isn't working as expected. The result of echoing $valor1; is ...

Unexpected issue encountered during the Angular 9 compilation process

Since migrating to Angular 9, I've encountered an issue with my feature branch after a rebase. Trying to switch to develop and update it using pull origin develop, everything seemed fine until I came across this error that's leaving me puzzled: h ...

Transforming the inputted URL into a clickable hyperlink

I have a text input field where any text entered is displayed below using angular.js. However, I am trying to convert any http URL entered into a clickable link. I found reference to the solution on this Stack Overflow page. Despite successfully converting ...

Tips on creating a View with flexbox in a React Native component

https://i.sstatic.net/nWdac.png Here is the layout I'm aiming for, with a focus on the products. This is what I tried: I created a component called "AllProducts" and set its flexDirection to row, then bound my Product Array to it. However, this was ...

Challenges with creating an increment and decrement form component using Vue

I've been working on this for almost three days now. My goal is to create a reusable component in Vue that allows a passed value to be incremented and decremented with the click of a button, and then submitted in a form. Below is my code: Parent Com ...

Encounter a Socket.io connection error due to net::ERR_CONNECTION_REFUSED when trying to access from multiple devices

I'm having trouble with other people's computers accessing my test chatting application. When they try to connect, they encounter this error: localhost:3000/socket.io/?EIO=4&transport=polling&t=Oi2Ko0C:1 Failed to ...

Transferring data between unrelated components

I am facing an issue where I am unable to pass a value from the Tabs component to the Task component. To address this, I have created a separate data service. The value in the Tabs component is obtained as a parameter from another component. However, when ...

Tips for displaying consecutive information from a Sequelize query with associations in Node.js

Attempting to create a straightforward sequential output from a demo Node.js app and Sequelize has proven to be challenging for me. I'm struggling to comprehend how promises and Bluebird can assist me in achieving the desired result: User: John Doe ...

How can I place a DOM element without relying on style properties?

Before diving in, let me provide some context. I am currently developing a visual element that operates similar to a spreadsheet. It features scrollable content areas in both directions, with axis indicators on the left and top that remain visible at all t ...

Issue with Angular 5 - Deselect all checkboxes not reflecting in the UI

I am currently working on integrating a reset button into a Reactive form in Angular 5. The reset functionality works flawlessly for all form fields, except for the dynamically created multiple checkboxes. Although it seems like the reset operation is hap ...

How can one efficiently modify data structures stored within Firebase?

I need to update the attribute contact in my JSON Store object to phonenumber. Here are my Store objects as displayed on Firebase dashboard: I want to change all instances of contact: to phonenumber: across all my Store objects simultaneously. Currently, ...

Can you guide me on how to record a value in Pulumi?

According to Pulumi's guidance on inputs and outputs, I am trying to use console.log() to output a string value. console.log( `>>> masterUsername`, rdsCluster.masterUsername.apply((v) => `swag${v}swag`) ); This code snippet returns: & ...

Loading a series of images in advance using jQuery

I have a series of frames in an animation, with file names like: frame-1.jpg, frame-2.jpg, and I have a total of 400 images. My goal is to preload all 400 images before the animation begins. Usually, when preloading images, I use the following method: v ...

What are the reasons for the success function not being called and what steps can be taken to correct it

Whenever I attempt to submit the post, the data is successfully sent and received by the server, but the success function never gets called. In the network inspector tab of Chrome, the connection shows as stalled with a warning: "connection is not finished ...

Access and interpret the data stored within a JSON file

I'm currently working on reading a JSON file into Python, but I'm encountering an error in a test script that follows my code. When my function is called, here is the code to read the JSON file. The data file is stored on the same server and the ...

Simulating service calls in Jest Tests for StencilJs

When testing my StencilJs application with Jest, I encountered an issue with mocking a service class method used in a component. The service class has only one function that prints text: The Component class: import {sayHello} from './helloworld-servi ...

Take command of the asynchronous loop

I have two Node.js applications that serve different purposes. The first is an Express.js Server (PROGRAM1), responsible for providing the user interface and RESTful APIs. The second is a web crawler (PROGRAM2), tasked with continuously reading items, do ...

Altering the href text within a script causes the text to disappear instead of updating

Function Triggered by Button Click: function LoadEnglishText() { document.getElementById("txt_whatwedo_learnmore2").innerHTML = "here."; } HTML Link to be Updated: <a id="txt_whatwedo_learnmore2" href="./pdf/Pricing_App_Dev_2019_Ger.pdf">hier ...

Passing data between screens in React Native can sometimes result in a TypeError where a function is not recognized. This error occurs when attempting to access an undefined variable while trying to pass data from one screen to another

I am working on developing a simple app, and one of the components in this app involves managing books. I have only 4 screens that utilize book objects, so I decided to use props to pass data between these screens. However, I encountered an error stating " ...