Only Return Values Found in an Array

Only Return Values Found in an Array

Hi all,

I have two REST calls and need to return the values that match (ex. If the first REST call output produces an array of 1, 2, 3... I want to the second REST call to output the values that match 1, 2, 3.)

My data looks something like this:

- Data source 1 accessed by REST call 1 has a field named PostCategoryID. This field can contain multiple values

- Data source 2 accessed by REST call 2 has two fields, one named ID and another named Title. The ID in list matches the value(s) in the PostCategoryID field of data source 1.

I need to output the Title from data source 2 where it matches data source 1.

Currently it displays all the Titles from data source 2, not just the ones I am trying to match against.

Here is my code. Please assist and thanks in advance:

  1. function yourcompanyNews()
    {
        var basePath = "/sites/resourcecenter/news/_api/";
        $.ajax({
            async: false,
            url: basePath + "web/lists/GetByTitle('Posts')/items?$filter=Placement eq 'YourCompany News'&$top=5&$orderby=YourCompanyNewsOrder asc",
            type: "GET",
            headers: { "Accept": "application/json;odata=verbose" },
            success: function (data)
            {
                //script to build UI HERE
                $.each(data.d.results, function (index, value)
                {
                    blogPostID = value.ID
                    categoryID = value.PostCategoryID
                    $("#YourCompanyNews").append('<p><a href="/sites/resourcecenter/news/Lists/Posts/Post.aspx?ID=' + blogPostID + '">' + value.Title + '</a>' + '</br>' + value.Placement + '</br>' + '<img src="' + value.Thumbnail.Url + '" /></p><p id="BlogPost' + blogPostID + '"></p>');














  2.                 $.ajax({
                        async: false,
                        url: "https://yourcompany.com/sites/resourcecenter/news/_api/web/lists/GetByTitle('Department or Region')/items",
                        type: "GET",
                        headers: { "Accept": "application/json;odata=verbose" },
                        success: function (categories) {
                            //script to build UI HERE
                            $.each(categories.d.results, function (index, value) {
                                if ($.inArray(value.ID, categoryID)) {
                                    $('#BlogPost' + blogPostID).append(value.Title);
                                }
                            });
                        },
                    });
                });
            },
            error: function (data)
            {
                //output error HERE
                alert(data.statusText);
            },
            error: function (data) {
                //output error HERE
                alert(data.statusText);
            },
            });
    }