0

I have this array of objects below and i want to iterate with $.each and get the type and description

Object[
 Object { 
   advert_id=7,
   type="entipo",
   description="magazine"
 }, 
 Object { 
   advert_id=8,
   type="tv",
   description="commercials"
 }
]
4
  • 1
    Looks like an array of objects to me... Commented May 21, 2015 at 14:02
  • Yes you are right, i'm fixing it right now. Commented May 21, 2015 at 14:03
  • Then where are you stuck, the DOC regarding $.each as some examples in it. BTW, what do you want to do with result? Commented May 21, 2015 at 14:03
  • I'm new to jQuery and i do not know hot to achieve that... Commented May 21, 2015 at 14:05

2 Answers 2

4

Let's assume it was assigned to variable MyObject. This should do it:

$.each(MyObject, function(index){
   console.log(MyObject[index].type);
   console.log(MyObject[index].description);
});

The above is rather tedious. Another way you could do it would be as follows:

$.each(MyObject, function(index, obj){
   console.log(obj.type);
   console.log(MyObject[index].description);
});

And finally, as follows:

$.each(MyObject, function(){
   console.log(this.type);
   console.log(this.description);
});

Of course you would replace console.log() with whatever it is you want to do with the values.

You may be wondering why you don't JUST use this? You may of course just use the approach that implements this, but the benefit of using one that has the index and the object in, is that you may need to programmatically do something with the index position of the object, in which case you have it easily accessible to you. That's not to say you could not easily get it when using only the this approach, but it makes life that much easier.

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

11 Comments

function(obj) obj is the index
Correct, I simply used obj as each index contains an object, which just makes more sense to me ;) Just a personal thing :p
use this instead of obj
@Makis - this simply references the current object in the loop as it iterates over all the objects. It's like looking through a stack of books and saying "I like THIS one, let me see how many pages THIS one has"
@Makis - I added 3 methods that you could use. You will see that in one I used index and obj as this follows the key => value notion, which is generally what arrays and objects consists of in the simplest form. Note, and array IS an object, I just decouple them as people often don't see them as being the same sort of thing. Each entry in the object has a key (or index) and the value in your case will be an object at each key. Hope that makes sense.
|
1

Another way:

$(yourArray).each(function(){
   console.log(this.type);
   console.log(this.description);
);

3 Comments

You shouldn't use $.fn.each on none jq array like object
That's why i first convert the Array to a jQuery object: $(yourArray). Its same as this $.each(yourArray,function(){}). Same thing, different syntax.
I missed that sorry but then it is quite useless anyway

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.