Skip to content
Advertisement

Split array data from fetch data

I fetch this data from the api

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

then i want to split this into two array

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

arrayNumber : [8646, 3451, 2342]

i try using split function but it didn’t work, when i check for array.length, console said it undefined.

when i console.log(array)

it show like this

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

Help me please.

Advertisement

Answer

First, This is not an array it’s an object.

You can use Object.keys and Object.value to get data

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);
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement