Looking for a simple method to convert JSON object properties into an array?

Is there a simple method for extracting only the values of the properties within the results object in the given JSON data?

let jsonData = {"success":true,
       "msg":["Clutch successfully updated."],
       "results":{"count_id":2,
                  "count_type":"Clutch",
                  "count_date":"2000-01-01",
                  "fish_count":250,
                  "count_notes":"test"}
      };

let extractedValues = extractValues(jsonData.results);
//extractedValues=[2, "Clutch","2000-01-01",250,"test"]

Answer №1

To create a function that extracts values from an object, you can use the following code:

const extractValues = (inputObj) => {
    let valueArr = [];
    for (let key in inputObj) {
        if (inputObj.hasOwnProperty(key)) {
            valueArr.push(inputObj[key]);
        }
    }
    return valueArr;
}

Answer №2

function transformResultsToArray (data) {
  var newArray = new Array();
  for (var index in data) {
    newArray.push(data[index]);
  }
  return newArray;
}

var resultArray = transformResultsToArray(j.results);

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

Using session variables to store connection objects for database rollback

My web application is divided into multiple modules spread across different JSP pages. Currently, I am facing the challenge of using separate oracle connection objects on each page due to scope limitations. The problem arises when I need to rollback databa ...

When using a jQuery AJAX call, the special character % is sent to the server side as %. This was successfully updated

I am working with a Java program that generates an HTML form populated with text from a database. The user can edit the text in the form, and upon submission, it is sent back to the Java backend using a jQuery AJAX call. The modified text is then saved in ...

What causes the order of `on` handler calls to be unpredictable in Angular?

Below is the code snippet I have been working on function linkFunc(scope, element, attr){ var clickedElsewhere = false; $document.on('click', function(){ clickedElsewhere = false; console.log('i ...

Issue: The initiation function fails to start data

I have created an app using Ionic framework that displays articles. When a single article is opened, I use the init function in the UserService to initialize user data: angular.module('coop.services') .factory('UserService', function( ...

Three.js is experiencing difficulties in loading textures for custom Geometry with ShaderMaterial

A Geometry (pyramid) is defined here with four vertices and 4 faces - var geom = new THREE.Geometry(); geom.vertices.push(new THREE.Vector3(0,100,0), new THREE.Vector3(-100,-100,100), new THREE.Vector3(0,-100,-100), new THREE.Vector3(100,-100,100)); geom ...

What steps do I need to take to ensure the progress bar extends all the way to the end of the sn

I am currently facing a challenge where the progress bar line in my message queue does not reach the end of the message before it closes. I have included a brief video showcasing the issue along with the relevant code snippet. Any suggestions or ideas woul ...

Unsupported data type in JSON for Spring MVC

My Spring MVC 3.2 application is experiencing issues with accepting JSON POST requests from browsers. Strangely, tools like CocoaRest work fine, but when using a browser or a tool like chrome://restclient/content/restclient.html, I encounter the following ...

Commit the incorrect file name with the first letter capitalized

There seems to be an issue with the git not recognizing the correct filename casing. I have a file named User.js in my workspace, but when checking the git status, it displays user.js instead. Despite repeatedly changing and committing as User.js, the gi ...

Problem with unique geometry and facial orientation

Just dipping my toes into ThreeJS and I'm encountering a small issue, likely due to incorrect usage. I'm attempting to create a custom geometry and manually define the faces normals. I've set one normal in one direction and another in the o ...

Having trouble with the open and create new post button not functioning properly

The submit post button, user name, and logout button are not functioning properly. Can anyone assist with this issue? F12 and jsintrc are not providing any useful information. Below is the HTML code for the create new post button which should direct to a ...

Creating an automated sequence of $http requests to sequentially retrieve JSON files with numbered filenames in Angular 1.2

I have a series of JSON files (page1.json, page2.json, etc.) that store the layout details for our website pages. Initially, I would load each page's data as needed and everything was functioning smoothly. However, it is now necessary for all page da ...

Extracting and storing a dynamically changing JSON file into a dictionary using Python

I am working with a dynamically changing json file, specifically at one point that changes unpredictably. My goal is to iterate through this changing section using a for loop in order to extract the necessary elements within that bracket of the json file. ...

Map with a responsive grid layout featuring two columns

My layout is currently set up with static markup that creates a flex design with 2 columns. <Row> <Col span={6}>content</Col> <Col span={6}>content</Col> </Row> <Row> <Col span={6}>content</Col> ...

Using React client to accept messages from a Socket.io server: A guide

I have a setup where a Node.js server with Socket.io is used to send messages between React clients. Currently, I can send a message from Client 1 to Client 2, but the recipient must click a button to view the message on their screen. I am trying to make i ...

Identify the credit card as either American Express or American Express Corporate

My JavaScript code can successfully detect credit card types, but I have encountered an issue. I am unable to differentiate between American Express and American Express Corporate cards. Is there a way to distinguish them or is it impossible to identify wh ...

Is it possible for asynchronous functions to write to the same object concurrently and result in invalid values?

I have been experimenting with the code below to see if it can cause any issues with the integer i in the object context. My tests so far have not revealed any problems. // Node.js (v10.19.0) const Promise = require('promise') // object access ...

The component "react-native-snap-carousel" is having trouble displaying the card image

Recently, I started working with react native and encountered an issue while trying to implement the "react-native-snap-carousel". The carousel was functioning correctly with the data provided in the "carouselItems" array, but I faced difficulties with dis ...

Error in Java DynamoDB Streams Lambda: Unable to convert a floating-point value to a date in type `java.util.Date` (token `JsonToken.VALUE_NUMBER_FLOAT`)

I've been working on an AWS Lambda function that listens for DynamoDB Streams events and then updates an Elasticsearch index accordingly. I decided to leverage the Quarkus framework for my code, and after doing some research, I came across a helpful r ...

How can I make Material UI's grid spacing function properly in React?

I've been utilizing Material UI's Grid for my layout design. While the columns and rows are functioning properly, I've encountered an issue with the spacing attribute not working as expected. To import Grid, I have used the following code: ...

Internal server error frequently occurs when there is an issue with Ajax requests in a Laravel application

Greetings, fellow developers! I am encountering an issue with the comments system in Laravel and Ajax. While it functions correctly with PHP alone, I am facing difficulties when using Ajax. The error message I am receiving is as follows: Status Code:50 ...