JavaScript Arrays and objects

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;
                newObject.push(flist.results[0])
                //
                for (var ptr=1; ptr<listlength; ptr++) {
                                if (flist.results[ptr].FID!=currentID) {
                                                newObject.push(flist.results[ptr]);
                                                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.
LVL 1
Howard BashSenior Software EngineerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Leonidas DosasCommented:
Would you like to give us an object sample to see the structure of the object that you want to dedup?
0
Slick812Commented:
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)
  idList.push(flist.results[i].FID);
  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"}

DedupeList(listObject);
/*
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,
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Slick812Commented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.