0

I have an object with these key-value pairs. This object comes from an API which I am calling :

APIName(obj).subscribe((res:any) => {
   console.log(Object.values(res.data));
})  



data = {
   0 : 1,
   1: 3,
   2: 7,
   3: 10
....so on
}

enter image description here

simply put it is an object of numbers and my desired output is (a simple array) :

data = [1,3,7,10]

I've tried Object.value and Object.key it still converts it into an object. Any help?

17

1 Answer 1

-1

First of all if your data is not sorted by key. Then sort it. and iterate through object and push to array.

var data = {
   1: 1,
   0: 3,
   2: 7,
   3: 10
};
let sortedKeys = Object.keys(data).sort();
let arrayOfData = [];
// iterate method
sortedKeys.forEach((key) => {
  arrayOfData.push(data[key]);
});
console.log(arrayOfData);

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.