1

In a list there are multiple items with same title . Now I want to loop those same name until new title found and this process with continue till the last item of the list . Finally each name will be printed without redundancy. Please help me with JSOM code.

1

Using this code you can get an array with the unique Title values from list

    $(document).ready(function () {
    // Ensure that the SP.js file is loaded before the custom code runs.
    SP.SOD.executeOrDelayUntilScriptLoaded(loadNewsData, 'SP.js');
});

This is used to get the data from list using REST service

/********************************************************************************************
* Function : loadNewsData                                                                   *
* Descritption : This function is used to assign the data to the respective div             *
*********************************************************************************************/
//Get today Date and Year
var todayDate = new Date();
var currentYear = todayDate.getFullYear();
function loadNewsData() {
    var listName = "News";
    var listDispName = "NewsandReferences";
    jQuery.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('" + listName + "')/Items",
        type: "GET",
        headers: { Accept: "application/json;odata=verbose" },
        success: function (data) {
            if (data.d.results.length > 0) {
                var results = data.d.results;                    
                var uniqueYear = new Array();                    
                $.each(results, function (index, dataNews) {

                    uniqueYear.push(dataNews.Title);

                });
                //Get unique years using GetUnique
                uniqueYear = GetUnique(uniqueYear);   

                //bind this array to varaible   - uniqueYear
                //Write your code   
            }                
        },
        error: function (jqxr, errorCode, errorThrown) {
            alert("KeystoneServiceDeskLinks.js:loadServiceData:: " + args.get_message());
        }
    });
}

This function used to collect the unique information

function GetUnique(inputArray) {
var outputArray = [];
for (var i = 0; i < inputArray.length; i++) {
    if ((jQuery.inArray(inputArray[i], outputArray)) == -1) {
        outputArray.push(inputArray[i]);
    }
}
return outputArray;}
  • bit overkill to use a jQuery function inArray where you can use native JavaScript indexOf That whole function can be shortened to: return inputArray.filter(function(elem,index,self){return self.indexOf(elem)===index}) which returns an array of unique values (.filter() has been around since IE9) – Danny '365CSI' Engelman Nov 16 '15 at 14:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.