0

i have a varable:

var name = "name";

this will be inside an array of objects i have:

    var all = [];
    $.each(results, function(i, item){
        all.push({
            rank: i + 1,
            username: item.TopicName,
            mentions: item.LastHourCount,
            totalcount: item.TotalCount,
            urlLg: item.LargeImageUrl,
            urlSm: item.SmallImageUrl,
            shortname: item.ShortName
        });

    });

I need to look through the array of objects and find the 'shortname' that matches the variable 'name'. the objects look like this:

Object[
  mentions: 21737
  rank: 2
  shortname: "name"
  totalcount: 59330
  urlLg: null
  urlSm: "http://i.cdn.turner.com/nba/nba/pulse/allstar/2012/img/images-small/howard.png"
  username: "lebron james"
],

once i find it set that to a variable: var showThis = all.[];

Inside the each function as it loops through the json file, is probably where to look for the name?

3 Answers 3

3

I think I may be misunderstanding. If you just want to find the entry in all with a shortName matching name, then:

var match;
$.each(all, function() {
    if (this.shortName === name) {
        match = this;
        return false;
    }
});

That uses $.each to loop through the array. In the iterator callback, this will refer to the array element, so we just check this.shortName === name. The return false stops the each loop. If there's no match, match will keep its default value (undefined).

Or as a traditional for loop:

var match, index, entry;
for (index = 0; index < all.length; ++index) {
    entry = all[index];
    if (entry.shortName === name) {
        match = entry;
        break;
    }
});
Sign up to request clarification or add additional context in comments.

4 Comments

Quick question, is there an easy way to then grab the next object after the match? So a user clicks next button, which will then load the next object?
@Xtian: Yes. If you read through the linked documentation, the iterator function we hand to each will receive two arguments (I didn't use them above) when called: The index of the item we're visiting, and a reference to it (e.g., function(index, item)). So if you find a match, then the next item after the match will be at all[index+1] (assuming the match wasn't at the very end).
this makes sense, i added a var matchNext and put this in my if statement: matchNext = match[i + 1]; but this isn't working?
@Xtian: See my comment above. You want to index into all, not match.
0

Underscore.js solution:

var showThis = _.find( all, function ( elem ) {
    return elem.shortname === name;
});

Plain JavaScript solution:

var showThis = all.filter( function ( elem ) {
  return elem.shortname === name;
})[ 0 ];

Btw, .filter() is an ES5 array iteration method. The ES5 array iteration methods are not implemented in IE8 and older versions. (You can easily polyfill them though.)

8 Comments

@Xtian: Note that Šime's used a feature of JavaScript taht won't exist on about half of the browsers people use in the wild; you'll have to shim it if you use it. Šime: meta.stackexchange.com/questions/118021/…
@T.J.Crowder "Half of the browsers" is a bit misleading. Only IE8 and IE7 are affected. Those two browsers comprise about 20-30% of page-views (depending on location).
@Šime: No, half is about right, and much closer than 20-30%. I did the math in my answer to the linked question. (Also, IE6 is affected; remember that more than a quarter of Chinese web surfers use IE6; a number which is thankfully finally coming down.)
@Šime: It's not just IE6. It's IE6, IE7, IE8, Firefox 3.x, and a bunch of others. You're casually ignoring no fewer than 30% of all web surfers even disregarding the Chinese market, and (all due respect) for no good reason. One sentence on an answer is what it takes to avoid setting a trap for someone to fall into. Why wouldn't you do that? Isn't the whole point of answering questions here to be helpful? Last word can be yours, I think we've beat this to death.
@T.J.Crowder Well, alright then. I'm going to include that note in my future answers.
|
0
var t={};

$.each(all, function(i, item){

  if (item['shortname']=="name") {
    t=this;
    return false;
  }

});

2 Comments

@T.J.Crowder yeah ... nevermind... if it was an html dom so i regullary make it jquery objkect....
And why use item in one place, but this in another?

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.