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?

Shinichi
  • 85
  • 1
  • 4
  • 13
  • Second result in Google; https://stackoverflow.com/questions/38824349/how-to-convert-an-object-to-an-array-of-key-value-pairs-in-javascript – jkulak Apr 15 '22 at 10:45
  • 2
    `data = Object.values(data)` should work. – Mohammad Usman Apr 15 '22 at 10:45
  • it's not working. I'll tell you what's exactly how it's not working. – Shinichi Apr 15 '22 at 10:47
  • @Shinichi - [`Object.values`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/values) works just fine for this. What specific problem did you have with it? – T.J. Crowder Apr 15 '22 at 10:48
  • @T.J.Crowder i've edited it. – Shinichi Apr 15 '22 at 10:50
  • @Shinichi - *"it still converts it into an object"* Arrays **are** objects, but `Object.values` gives you an array, not just a non-array object. Here's an example: https://jsfiddle.net/tjcrowder/ugx2dc7o/ – T.J. Crowder Apr 15 '22 at 10:52
  • @T.J.Crowder I've added the pics. Please check. – Shinichi Apr 15 '22 at 10:54
  • @Shinichi - The pics show that the "object" is **already** an array. There's no need to attempt to convert it further. – T.J. Crowder Apr 15 '22 at 10:58
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/243928/discussion-between-shinichi-and-t-j-crowder). – Shinichi Apr 15 '22 at 10:59
  • @Shinichi - I see no point in doing so. You already have an array. That's your answer. If there's further clarification you want to make, do it with an edit to the question or a comment (since it's directly related to the question, not a "discussion" -- the site feature that suggested you do that is one of the dumber things they did to the site :-) ). – T.J. Crowder Apr 15 '22 at 11:00
  • Side note: The code in the question is trying to use `res.data`, but the screenshot shows `res.data.afterRelease`. – T.J. Crowder Apr 15 '22 at 11:01
  • @T.J.Crowder , The when i checked the typeof of the variable it showed me 'Object'. That's the reason i asked was due to that. P.S I know its res.data.afterRelease I just did it for making it simple. – Shinichi Apr 15 '22 at 11:07
  • @Shinichi `typeof []` is indeed `"object"`. `typeof` isn't very useful, sadly. To see if something is an array, use `Array.isArray(theThing)`. MDN's reference material for JavaScript is very good: [`typeof`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof), [`Array.isArray`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray) – T.J. Crowder Apr 15 '22 at 11:08
  • 1
    @T.J.Crowder oh ok. I understood that now. Thanks a lot for your help. – Shinichi Apr 15 '22 at 11:09
  • @Shinichi - I'm glad we figured it out! :-) – T.J. Crowder Apr 15 '22 at 11:10
  • @T.J.Crowder I'm trying. Now I've to convert it into Array of strings i.e. data = ['1','3','7'] – Shinichi Apr 15 '22 at 11:12
  • @Shinichi - See [`map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map). The [`Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array) page has lots of useful info. Then for converting numbers to strings, there are lots of questions and answers on that, such as [this one](https://stackoverflow.com/questions/5765398/whats-the-best-way-to-convert-a-number-to-a-string-in-javascript). – T.J. Crowder Apr 15 '22 at 11:15

1 Answers1

-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);
Nbody
  • 1,168
  • 7
  • 33