1

I have the following very massive array:

var arrayBig = {"count":31,"items":{"76":{"title":"office1","address":"<p><strong>London<\/strong><\/p>\r\n<p>Baker Str.<\/p>\r\n<p>Web site: <a href=\"http:\/\/example.org\">http:\/\/example.org<\/a><\/p>"},"57":{"title":....... etc. }....

And all I need is to get the first number in 'items', e.g. 76, with jQuery.

8
  • 1
    It's not an array. To access object attributes use . or [] operators. Commented Dec 23, 2014 at 8:52
  • You can get it with object.keys() method of JS. Commented Dec 23, 2014 at 8:52
  • 1
    @Alexander Arutinyants: it worth mentioning that properties of objects are not ordered. And the standard DOES NOT guarantee any order. So technically - it's impossible to retrieve the first attribute of an object. Commented Dec 23, 2014 at 8:53
  • Thanks. I know how to get 'title' or 'address'. But don't know how to get the number in the beginning - it has no name. Commented Dec 23, 2014 at 8:54
  • Ok. I see, first you wanna do search by any field, then get the number? Commented Dec 23, 2014 at 8:55

2 Answers 2

1

try

var arrayObj = [];
    $.each(arrayBig.items, function(key, val) {

           arrayObj.push(key);

        });​

alert(arrayObj[0]);
Sign up to request clarification or add additional context in comments.

3 Comments

How does this code take only the first value?
store all values in array and take array[0]....simple.
You're not doing either of those in your answer.
1

Just try with:

var firstKey = Object.keys(arrayBig.items)[0];
arrayBig.items[firstKey]

3 Comments

Order of properties IS NOT DEFINED and IS NOT GUARANTEED.
@zerkms it returns first key anyway. If it should be the lowest key in group, array of keys should be sorted.
"it returns first key anyway" --- it returns for this particular run for this particular version of ECMAScript implementation. Nothing prevents the JS VM to change the object layout for memory optimization purposes. It's like crazy to rely on something that is not standardized and is not guaranteed.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.