0

I use this code

labels.map((label, key) => {
  const xAxis = (parseInt(label, 10)).toFixed(0);
  const yAxis = parseInt(this.props.dataSet.datasets[0].data[key], 10);
  const arrayAxis = [xAxis, yAxis];
  data.push(arrayAxis);
  return arrayAxis;
});

then i get the data list

["1", 1] 
["2", 1]  // duplicate
["2", 1]  // duplicate
["3", 1] 
["4", 1] 
["4", 1] 
["5", 4] 
["6", 4] 
["7", 4]
["8", 4]
["9", 4]
.....
....
...

How I can remove duplicate array from the array list using filter?

4
  • what is a duplicate? please add the raw data of label. Commented Jun 10, 2016 at 7:21
  • @NinaScholz U can see in data list Commented Jun 10, 2016 at 7:23
  • are the data ordered? Commented Jun 10, 2016 at 7:27
  • ya data is ordeerd Commented Jun 10, 2016 at 7:33

5 Answers 5

2

You could use this with a temporary object for indicating duplicates. It works for non sorted arrays as well.

levels.forEach((temp => (label, key) => {
    var xAxis = (parseInt(label, 10)).toFixed(0),
        yAxis = parseInt(this.props.dataSet.datasets[0].data[key], 10),
        arrayAxis =[ xAxis, yAxis];
    if (!temp[arrayAxis]) {
        temp[arrayAxis] = true;
        data.push(arrayAxis);
    }
})(Object.create(null)));
Sign up to request clarification or add additional context in comments.

Comments

0

You can do it with filter, but it would be much simpler using lodash's uniqBy and isEqual functions.

_.uniqWith(arr, _.isEqual);

You can read about it here: https://lodash.com/docs#uniqBy https://lodash.com/docs#isEqual

Notice that it does involve adding a dependecy on lodash.

Comments

0

Use this:

var data = [["1", 1],
["2", 1],
["3", 1],
["4", 1],
["5", 4],
["6", 4],
["7", 4],
["8", 4],
["9", 4]];

const xAxis = "2";
const yAxis = 1;

const arrayAxis = [xAxis, yAxis];
if($.inArray(arrayAxis[xAxis], data) !== -1)
{
    data.push(arrayAxis);
}
console.log(data);

This code prevents your duplicates.

Comments

0

If possible, I always like to provide a generic answer rather than a tailored one to the specific question.

To me this question is about filtering the array or object items of an array according to not a simple property but a similar array or object. I would use two utility methods those i had used at some other cases previously. Object.prototype.compare() and Array.prototype.compare(). Lets see.

Object.prototype.compare = function(o){
  var ok = Object.keys(this);
  return typeof o === "object" && ok.length === Object.keys(o).length ? ok.every(k => this[k] === o[k]) : false;
};
Array.prototype.compare = function(a){
  return this.every((e,i) => typeof a[i] === "object" ? a[i].compare(e) : a[i] === e);
};

var data = [["1", 1],["2", 1],["2", 1],["3", 1],["4", 1],["4", 1],["5", 4],["6", 4],["7", 4],["8", 4], ["9", 4]],
filtered = data.filter(function(e) { var f = this.find(f => f.compare(e));
                                     return !f ? this.push(e) : void 0;
                                   },[]);
console.log(JSON.stringify(filtered));

Comments

0
let res = [...new Set([1,3,45, 1, 3])]

output is = [1,3,45]

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.