Get the index of certain value of JSON Objects

Viewed 1849

I have json data that I am pushing to an array. I need to get the index value of a certain ID value. I have tried IndexOf but it doesnt seem to work.

I have attached a snippet of my code and array. Along with a link to the JSON file for you to use.

I have added a comment to highlight the problem area. I have also sent a screenshot of the array being console.logged and the value I need to get.

var posts;
var imgTitle;
var imgLink;
var url = 'http://www.capetownetc.com/api/get_category_posts/?slug=';
var counter = 1;
var postID;
var callFunction = true;
var articleContent;
var titleMaxLength = 28;
var articleArray = [];
var topicName = 'news';
var pageNumber;
var linkID;

//POST CARDS

function postCards(pageNumber, cardLimit, container, topic) {

    topicName = topic || 'news';
    pageNumber = pageNumber || 1;
    
    $.ajax({
        type: 'GET'
        , url: url + topicName + '&count=10&page=' + pageNumber
        , data: {
            get_param: 'value'
        }
        , dataType: 'jsonp'
        , success: function postPop(data) {
            $('.main-content').append('<div class="card-container card-container' + container + '"></div>');
            $.each(data.posts, function(i){
            
                posts = data.posts[i];
                imgTitle = data.posts[i].title;
                imgLink = data.posts[i].thumbnail_images.medium.url;
                imgTitle = imgTitle.substr(0, titleMaxLength);
                postID =  data.posts[i].id;
                
                $('.card-container' + container).append('<a href="' + postID + '" class="article-link" onclick="postArticle(' + i + ',' + pageNumber +')"><div class="card card1"><img src="' + imgLink + '" class="card-img"><span><h2 class="card-heading">' + imgTitle + '</h2></span></div></a>');
                
                $('#loading').css('opacity' , '0');
                
                callFunction = true;
                
                //JSON CONTENT TO ARRAY
                articleArray.push(posts);
                //**PROBLEM AREA - I need to get the ID position of the posts in this array, see    screenshot -
                if(window.location.hash) {
                
                    mainView.router.load({
                        content: newPageContent,
                        animatePages: true
                    });

                    articleContent = articleArray[i].content;

                    $('.article-container').html(articleContent);
                    console.log('hash');
                } 
                else {
                    
                }

            });
            console.log(articleArray);
        }
        
        }).done(function(){
            myApp.hidePreloader();
        });
        myApp.showPreloader();
}

postCards(counter, 10, counter);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
                            
                            <div class="main-content">

                            </div>

JSON Data File

The value which position I need to get

1 Answers
Related