2

I have json with array of objects in it. I build my page depends on elements in this array. If there is no duplicate values of key called points, i render page with some info and description, using value of points to find this element in array. However if i have 2 and more duplicate values of key called points i render list of these elements. In this case i cant use value of points to find element in array. I know i can use index number of array element, and then pass it as parameter to my function that find and build info and description, but i'm not sure how to do that. How do i get index number of element in array?

P.S. Can provide my code if needed

Code that i'm using

var allRewards = null;
$("#reward").live('pagecreate', function(e) {
    var request = $.ajax({
        type: "GET",
        url: "example.com/test.json"
        dataType: "json",
        error: function (data, textStatus){
      console.log( "it`s error" );
      console.log( status );
      console.log( data );},
        success: function (data, textStatus){

        console.log( "success" );
        console.log( status );
        console.log( data );
         }
        })
        request.success(function(data, textStatus){
            var lis = "";
            var arr = [];
            var iter = 0;


            allRewards = data

            $.each(data.rewards, function(key, val){
                if ($.inArray(val.points, arr) == -1)
                    {
                        lis += "<div data-points='"+ val.points +"'align=CENTER class = 'rewards-block ui-block-" + String.fromCharCode(97 + iter%3) + "'><a href ='#' class ='ui-link-inherit' onclick='showreward("+val.points+")'><img src ='./img/reward-icon.png'/><span>" + val.points + "&nbsp;pts</span></a></div>";
                        arr.push(val.points);
                        iter +=  1;
                    }
            });
            $("#rewards_table").html(lis);

        })

      });

function showreward(point)
{   
    $.mobile.changePage('show-rewards.html')
    console.log(allRewards);
    $("#showrewards").live('pagecreate', function(e) {
                var items = "";
                var arr = [];
                var counter = 0;
                var result = $.grep(allRewards.rewards, function(e){ return e.points == point; });
                        if (result.length > 1)
                {
                    $.each(result, function(key, val){
                        items += "<div style='color:white;'>" + val.title + "</div>"
                        console.log(val.title);
                    })
                }
                else if (result.length == 1)
                { 

                    // $.each(result, function(key, val){
                    //                      items += "div style='color:white;'"+ val.points + "></div>"
                    //                      console.log(val.points);
                    //                  })
                    $.each(result, function(key, val){
                        items += "<div style='background:white; padding:5px 5px 20px 5px;'><img style ='float:right; width:45%; margin-top:22px; padding: 0 0 10px 10px;' src ='" + val.main_photo_url + "'/><h3>"+ val.title + "</h3><p>" + val.description + "</p><p style='font-weight:bold; font-size:13px;'>Reedem below for " + val.points + " Zingle Points</p><table class='pagemenu' style='width:200px;'><tr><td class='width_5'><input type='submit' data-theme='w' value='Reedem Now' data-inline='true'></td><td><a data-role='button' data-icon='pagemenu-share' data-iconpos='notext' href='index.html' data-shadow='false' data-corners='false'></a></td></tr></table></div>"
                    });
                }
                console.log(items);
                $("#rewards-list").html(items);

});

}
4
  • Please provide some code Commented Dec 4, 2012 at 8:09
  • added. In that code i build page with info and description, but not sure how to get object that i need if i build list of items with duplicate key:points Commented Dec 4, 2012 at 8:14
  • You could add in a hidden field that has the value of the index, that way you can access this easily by using $(this).find('input[type=hidden]').val() You could also store the index as a part of your class or ID in the parents div. Commented Dec 4, 2012 at 8:19
  • That would be option, but i didnt look into my json, which contains unique ids of elements, with help of which i can access to any element. Commented Dec 4, 2012 at 8:21

1 Answer 1

3

I think you're looking for Array.indexOf.

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/indexOf

PS. This is available in Underscore as _.indexOf.

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.