Separating the array data retrieved from fetching the data

Getting data from the api is my task

array : { "Blue": 8646, "Red": 3451, "Green": 2342}

I then aim to divide this into two separate arrays

arrayColor : ["Blue", "Red", "Green"]

arrayNumber : [8646, 3451, 2342]

Attempting to split using the split function was unsuccessful, as when I checked for array.length, the console returned it as undefined.

When I console.log(array)

the output appears like this

Proxy { "Blue": 8646, "Red": 3451, "Green": 2342}

Please assist me with this issue.

Answer №1

Initially, it's important to note that this structure is not an array but actually an object.

To extract the data, you can utilize Object.keys and Object.value methods.

const obj = { Blue: 8646, Red: 3451, Green: 2342 };

const color = Object.keys(obj);
const value = Object.values(obj);

console.log("color :", color);
console.log("value :", value);

Answer №2

A JSON object consists of a key and its corresponding value pair, with the key representing a color and the value being a number. {"key":"value"}

If you need to address your problem, you can try the following approach:

let data = {"Blue": 8646, "Red": 3451, "Green": 2342};
let colorsArr = [];
let numbersArr = [];
for(let prop in data) {
    console.log(prop);
    colorsArr.push(prop);
    numbersArr.push(data[prop]);
}
console.log(colorsArr);
console.log(numbersArr);

Answer №3

The issue with the split function and length not functioning correctly is due to the fact that the provided data is an object rather than an array. The split function is designed to work specifically with arrays. It appears that the code given above is correct.

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

Restore font-weight if a different list item is chosen

I previously inquired about setting the font-weight to bold on a text element when selected. Thanks to @Eric, this has been successfully implemented! However, I am facing an issue where clicking on one text makes it bold, and then clicking on another text ...

Processing .obj file and converting it into a string format with the help of three

I have a unique challenge where my program creates .obj files at runtime and stores them as strings. I am now looking to load these generated .obj files using the load() function in three.js (OBJLoader). Currently, the load() function uses an HTTP request ...

Encountering an error while attempting to access a property of a non-object in the

Recently, I have been exploring the world of JSON. After retrieving a JSON object from the database, it appears in this form: Array ( [0] => stdClass Object ( [id] => 1 [data] => {"vehicle":[{"year":"2000","mak ...

Developing dynamic objects for input string fields in AngularJS

In my AngularJS view, I have the following setup: <label class="control-label">Name:</label> <input type="text" class="form-control" ng-model="config.name" /> <br /> <label class="control-label">versionSpecificApiConfig:&l ...

Utilizing jQuery to search and modify multiple attributes simultaneously

As a beginner in the world of JavaScript, I'm struggling with grasping the syntax. This is the script I've written: jQuery(document).ready(function ($) { $('div[align="right"][style="margin-right:15px;"]').each(function () { ...

Refresh all the data points displayed on the c3.js chart

I created a basic div element in my HTML code: <div class='all'> Show all </div> Accompanying that, here is the JavaScript code I implemented: $(document).ready(function () { $('.all').click(function(){ ch ...

Converting DateTime to text in PHP

I'm facing a challenge where I need to separate my datetime value (stored in database) using PHP. $temps[0]['Date_deb']; // --> "2017-10-07 00:00:00" $letemps = $temps[0]['Date_deb']; //echo $letemps->format(&a ...

Utilizing the power of Math.max operators to identify the two highest numbers within an array

I'm currently working on a Java project that involves finding the top two times in a marathon. I've set up arrays to store the data, but now I need to figure out how to identify the highest and second-highest values within the array. Is it possib ...

What is the method to prolong the end date of fullCalendar.js through Javascript?

I am facing the same issue as this individual. I am unsure of how to add one day to the end date. I do not want to alter the database value, only modify it on the HTML page. Currently, this is my calendar (no Moment.js joke intended): $(document).ready(f ...

Update the checkbox values for all checkboxes within the grid

Need help with setting the checkbox value of all checkboxes in a column based on a header checkbox. The function below is called when the column header checkbox is clicked and takes the column header name and the checkbox object as arguments. function han ...

Incorporate an additional column into a table using AngularJS

I attempted to insert a new column upon clicking a button, but unfortunately, I was unsuccessful. I have created a JSFiddle demo to illustrate my issue. Any assistance in resolving this problem would be greatly appreciated. Below is my attempt: $scope.a ...

Problem: Toggle functionality not working properly in JavaScript

As a beginner, I am facing an issue with multiple toggle buttons. They work fine individually, but when nested within each other, only one toggle button works. After some research, I realized that I need to make adjustments to my JavaScript code. However, ...

The interconnected nature of multiple asynchronous tasks can lead to a render loop when relying on each other, especially when

My asynchronous function fetches data in JSON format from an API, with each subsequent call dependent on the previously returned data. However, there are instances where I receive null values when trying to access data pulled from the API due to the async ...

Dragging elements with jQueryUI multiple times

Is there a way to configure drag and drop functionality so that one element can be dragged multiple times? I attempted to create something similar to this http://jsfiddle.net/28SMv/3/, but after dragging an item from red to blue, the element loses its abi ...

PHP: Function similar to an array builder

After a long break from using PHP, my focus has shifted to C, C++, Objective-C, Ruby & ECMAScript. However, I'm taking the opportunity tonight to keep my mind fresh by delving into some different languages. While browsing PHP.net, I stumbled upon an ...

What is the proper way to utilize props.theme.breakpoints in conjunction with the makeStyles hooks?

Can anyone help me understand how to incorporate the Material breakpoints provided at https://material-ui.com/customization/breakpoints/ into the makeStyles hook for responsive styling? I am having trouble using props.breakpoints.down('600') with ...

Sending a 2D array by value does not function as intended

I am facing an issue with passing a 2D array to a function without modifying the original array. Even though I am trying to pass it by value, my actual array is still getting modified when changes are made in the function. Why does this happen? int main(i ...

Error encountered while parsing JSON on iOS due to an empty object key

Hello, I've been working on parsing a JSON using NSDictionary in order to access specific values. However, I've encountered an issue where there is no key present as I delve deeper into the JSON structure. Is there a way to bypass this key and na ...

Methods for verifying the device on which a web application is currently operating

After developing a web application using node.js, express, angular, and bootstrap, I noticed that the layout and forms were not displaying correctly on all devices. Specifically, when running the app on a different device, such as an iPad or laptop, the fo ...

Spring Boot is having trouble identifying the JavaScript files

I've incorporated Spring Boot in my project and I'm working with a JSP file that looks like this: <%@ include file="common/header.jsp" %> <%@ include file="common/navigation.jsp" %> <div class="container"> ...