-2

I have 3 arrays like this,

var data1 = ['a','b','c'];
var data2 = ['b','c','d'];
var data3 = ['c','d','e'];

then I need to push unique value in to new data var newData = [];

I need result in my newData have only 'a','b','c','d','e' without duplicate

  • Possible duplicate of [Unique values in an array](https://stackoverflow.com/questions/1960473/unique-values-in-an-array) – Trent Jul 26 '17 at 03:58

5 Answers5

2

Try this

    var data1 = ['a','b','c'];
    var data2 = ['b','c','d'];
    var data3 = ['c','d','e'];
    var newArr=[]
    data1.concat(data2,data3).forEach(function(a,i){
      if(newArr.indexOf(a)===-1){
      newArr.push(a)
      }
    })

console.log(newArr);
Sourabh Somani
  • 2,138
  • 1
  • 13
  • 27
2

Modern browser?

var newData = Array.from(new Set(data1.concat(data2, data3)));

or

var newData = [...new Set(data1.concat(data2, data3))];
Jaromanda X
  • 53,868
  • 5
  • 73
  • 87
0

Try this Example

var data1 = ['a','b','c'];
var data2 = ['b','c','d'];
var data3 = ['c','d','e'];
var finaldata = data1.concat(data2,data3);

var unique = finaldata.filter(function(elem, index, self) {
      return index == self.indexOf(elem);
});
console.log(unique);
HARDIK
  • 72
  • 8
0

You want values from all three arrays but only unique values. That's a Set. ES2015 introduced sets, which are spec'd to use performant (log or constant time) access and checking.

var data1 = ['a','b','c'];
var data2 = ['b','c','d'];
var data3 = ['c','d','e'];

const set = new Set(data1.concat(data2).concat(data3))

const nonDuplicates = [...set]

console.log(nonDuplicates)
Gabriel L.
  • 1,629
  • 1
  • 17
  • 18
0
const newData = [...new Set(data1.concat(data2, data3))]; 
Nic Bonetto
  • 543
  • 1
  • 8
  • 20