0

I've a jQuery object (counts) like this :

Object {men: 299, womens: 175, children: 173} 

I just want to retrieve the value to display it in a span (the data-filter attributes are the same as the object keys)

$( "a.type_select" ).each(function() {
    var theType = $(this).attr('data-filter');
    var theCounter = counts.theType;
    $(this).children('span.count').html(' ('+theCounter+')');
});

How can I do this ?

Thanks !

1 Answer 1

1

counts.theType access the key theType of the object counts.

It is important to know that object key can also be accessed by string. You just need to use bracket. for example :

counts['men']; // 299

That being said, you can pass a variable to the bracket :

counts[theType]

That will return what you want.

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

Comments

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.