JavaScript Arrays and objects

Howard Bash
Howard Bash used Ask the Experts™
I am getting back an object from a rest call which contains results from a SharePoint REST call.  The returned data has dups (which are indeed in the list).

I want to dedup the data and then return it to be used elsewhere.  It looks something like the following:

function DedupeList(flist) {
                var listlength = flist.length;
                var newObject=[];
                var currentID = flist.results[0].FID;
                for (var ptr=1; ptr<listlength; ptr++) {
                                if (flist.results[ptr].FID!=currentID) {
                                                currentID = flist.results[ptr].FID;
                return newFilmsObject;                

Open in new window

The issue is I want to return an object like the passed in data set from the REST call and am not sure if the array I am using to build the deduped set will work.
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Distinguished Expert 2017

Would you like to give us an object sample to see the structure of the object that you want to dedup?
greetings Howard Bash, , , , You might could duplicate the    flist   object, and remove the duplicate FID from it, But I feel it would be easier to remove the duplicate FID from the flist.results array, here is some code that works in fire fox -

function DedupeList(flist) {
// in javascript objects and arrays are passed in function parameters as a reference
var dups = [], idList = [flist.results[0].FID];

for (var i=1; i < flist.results.length; ++i) {
if (idList.indexOf(flist.results[i].FID) == -1)
  else dups.push(i);

// I had to use dups as an array position storage to maintain the original order in results
for(i=dups.length-1; i > -1 ; --i) flist.results.splice(dups[i], 1);
// since the flist is a reference you do NOT need to return it from function

var listObject = {results: [{FID: "id001", cost: 23.45},{FID: "id002", cost: 13.50},{FID: "id003", cost: 3.95},{FID: "id004", cost: 33.20},
{FID: "id002", cost: 13.50},{FID: "id002", cost: 13.50},{FID: "id005", cost: 65.75},{FID: "id004", cost: 33.20},{FID: "id006", cost: 2.95}], 
owner: "Fred", date: "yesterday"}

since the listObject is passed by reference, the function CHANGES it, 
the dups are not in listObject after the function
before function - listObject.results[4].FID was "id002"
after function - listObject.results[4].FID is "id005"

Open in new window

I hope this will give you idea of a way to do this,
This question has been inactive for 14 days, with no comment from the author. Force Close this question, answer to code that gives a duplicate cleared array.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial