0
var arr = [
    {level:0,name:"greg"},
    {level:0,name:"Math"},
    {level:0,name:"greg"}
];

I have tried the following:

function removeDuplicates:(dataObject){
    self.dataObjectArr = Object.keys(dataObject).map(function(key){
        return dataObject[key];
    });

    for(var i= 0; i < self.dataObjectArr.length; i++ ){
        self.dataObjectArr[i]['name'] = self.dataObjectArr[i];

        self.uniqArr = new Array();
        for(var key in self.dataObjectArr){
            self.uniqArr.push(self.dataObjectArr[key]);
        }
    }
    self.uniqObject = DataMixin.toObject(self.uniqArr);
    return self.uniqObject;
}

But I get error saying: Uncaught TypeError: Converting circular structure to JSON.

3
  • 1
    Should Math be "Math"? Commented Sep 28, 2016 at 7:10
  • Seems your function declaration has an : here -> function removeDuplicates:(dataObject) ? Commented Sep 28, 2016 at 7:10
  • You can use underscore check this answer stackoverflow.com/questions/9923890/… Commented Sep 28, 2016 at 7:14

5 Answers 5

2

You should push the name to an array or a set and check the same in the following..

var arr = [{
  level: 0,
  name: "greg"
}, {
  level: 0,
  name: "Math"
}, {
  level: 0,
  name: "greg"
}]

function removeDuplicates(arr) {
  var temp = []
  return arr.filter(function(el) {
    if (temp.indexOf(el.name) < 0) {
      temp.push(el.name)
      return true
    }
  })
}

console.log(removeDuplicates(arr))

Sign up to request clarification or add additional context in comments.

7 Comments

What is temp.indexOf(el.name) < 0 doing?
less than 0 mean -1(not found)?? Could you please explain
Exactly, if not found, indexOf() will return -1
I know that but how is it removing duplicates with this logic I am not able to understand
I'm not returning false explicitly. If nothing is returned, it'll be taken as undefined, which is the same like false
|
2

Here's a generic "uniquify" function:

function uniqBy(a, key) {
    var seen = new Set();
    return a.filter(item => {
        var k = key(item);
        return !seen.has(k) && seen.add(k)
    });
}

///

var arr = [
    {level:0,name:"greg"},
    {level:0,name:"greg"},
    {level:0,name:"joe"},
    {level:0,name:Math},
    {level:0,name:"greg"},
    {level:0,name:"greg"},
    {level:0,name:Math},
    {level:0,name:"greg"}
];

uniq = uniqBy(arr, x => x.name);
console.log(uniq);

See here for the in-depth discussion.

Comments

0

I believe you have a syntax error " removeDuplicates:(dataObject){ ..."

should be without the ":" >> " removeDuplicates(dataObject){ ... " "

You can try this :

function removeDuplicates(arr){
    var match={}, newArr=[];
    for(var i in arr){ if(!match[arr[i].name]){ match[arr[i].name]=1; var newArr=i; } }
    return newArr;
}

arr = removeDuplicates(arr);

Comments

0

You can use $.unique(), $.map(), $.grep()

var arr = [
  {level:0,name:"greg"},
  {level:0,name:"Math"},
  {level:0,name:"greg"}
];

var res = $.map($.unique($.map(arr, el => el.name)), name => 
            $.grep(arr, el => el.name === name)[0]);

jsfiddle https://jsfiddle.net/4tex8xhy/3

Comments

0

Or you can use such libraries as underscore or lodash (https://lodash.com/docs/4.16.2). Lodash example:

var arr = [
    {level:0,name:"greg"},
    {level:0,name:"Math"},
    {level:0,name:"greg"}
];

var result = _.map(_.keyBy(arr,'name'));

//result will contain
//[
//   {
//    "level": 0,
//    "name": "greg"
//    },
//    {
//    "level": 0,
//    "name": "Math"
//    }
//]

Ofc. one thing to always consider in these tasks, what do you want exactly are you going to do: modify an existing array, or get a new one back. This example returns you a new array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.