How to get the first item of an object in javascript?

Asked

Viewed 4,110 times

4

I have the following object in javascript:

{
  "tipo_entrega_id" : ["Valor não é válido"],
  "outro_campo"  : ["valor deve ser preenchido"]

}

The values are not defining by me, they come dynamically through a response to an ajax request.

I always need to take the first value of this object (without knowing what the key is) dynamically.

How do I do this in Javascript?

  • you refer to first as who says "any"? or you expect those properties to have an order?

  • "". Because PHP gives me an error in json, as I described above. Then I just want to display the first one (any first one) of the list

  • Hmmm... that doesn’t make much sense, to give any, cross-browser, you can do like this: https://jsfiddle.net/ov5ym27L/ but if you don’t know what the first thing is that it’s useful to debug?

  • @Sergio is because if the request returns an error, it returns the same as the object I showed above. Any error that displays to me is fine! If all goes well, there are no mistakes, then my ajax "Success" runs smoothly

  • "if the request returns an error, it returns equal to the object I showed above" - it is the server, the browser or a JS library?

  • @Sergio I use Laravel. Laravel returns a JSON (status 422). From there on errorfrom the ajax I capture the error.responseJSON. It comes with the above object, but I only need one error message, not all. As the error is already automatic of the system, instead of changing everything that is already ready, I preferred to do so in this part.

  • And why not choose one of the keys if you already know the structure of the object, instead of "drawing" a key?

  • @Sergio because one time can come a key, another time can come another. For example: If I have the fields "tipo_id" and "note", if I fill in "tipo_id" and forget "note, it will return the key "note" and the errors in a Array. If it’s the other way around, you can see "tipo_id". If you stop filling both, then both will come. It’s a validation.

Show 3 more comments

3 answers

3


The method Object.keys() returns an array of enumerable properties of a given object in the same order it is provided by a loop for...in (the difference is that a tie for-in lists properties that are in the prototype chain).

Syntax

Object.Keys(obj)

Parameters

obj

The object whose properties are enumerable.

Description

Object.Keys() returns an array whose elements are strings corresponding to the enumerable property found directly on the object. The ordering of properties is the same as that given by loop about the properties of the object manually.

Examples

var obj = {
  "tipo_entrega_id" : ["Valor não é válido"],
  "outro_campo"  : ["valor deve ser preenchido"]

}
console.log(Object.keys(obj)[0]); 

2

Underscorejs

If you have the habit of using Undescorejs, it is possible to do so:

var primeiro = _.first(_.toArray(objeto));

First we convert the Object for Array and then we took the first item with the function _.first.

Javascript Puro

With javascript, because some browsers do not support some things, you can use the for and give a break in the first iteration. So you take the first item.

function firstInObject(obj)
{
        for (var key in obj) return obj[key];
}

2

Can do:

var obj = { tipo_entrega_id: 10, outro_campo:123 };
var val1 = obj[Object.keys(obj)[0]]; // criar um array com as keys do obj, e depois vamos só buscar a primera, com index 0
console.log(val1); // 10

  • Great, you can do it in "one line"

  • You’re welcome, I put a commented explanation in the code in case you’re interested @Wallacemaxters

  • In fact, you explained in the :-D comment

  • Does not work in IE <9

Browser other questions tagged

You are not signed in. Login or sign up in order to post.