0

The backend gave me a strange websocket to work with where the array data of [2,4,5,1,3,5,...] has been processed as {'00:00':{...'count':2...}...}...

So first I need to count the length of an object, and when I use object.length, it returns undefined...

And then I need to recreate an array by using:

var newArray = [];
for(var i = 0; i < object.length; i++){
  newArray.push(object[i].count);
}

My question is how to find the length of an array with names and keys and select them with index?

It looks like I have to first get an array from an object with Object.keys or Object.values and then work from there.

Aero Wang
  • 8,382
  • 14
  • 63
  • 99
  • 2
    Don't really get the question, but is something like [Object.keys()](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/keys) what you're after? – George Apr 10 '17 at 11:02
  • what does console.log(array) say? – binariedMe Apr 10 '17 at 11:02
  • Its not an array its an object, the way to know its length is to walk it: [Javascript iterate object](http://stackoverflow.com/questions/14379274/javascript-iterate-object) – Alex K. Apr 10 '17 at 11:02
  • Okay guys let me give it a try. Sorry I will fix the terminology in a moment. – Aero Wang Apr 10 '17 at 11:03
  • 1
    If you would like to use a `for` loop then it should be like in the form of `for (var key in obj) doSomethingWith(obj[key]);` – Redu Apr 10 '17 at 11:42

1 Answers1

0

It looks like I have to first get an array from an object with Object.keys or Object.values and then work from there.

Aero Wang
  • 8,382
  • 14
  • 63
  • 99