Mohammed Hablullah
Mohammed Hablullah

Reputation: 105

How to get the common values in an array

For example i am having an array of data as below

var arrData = ["40-25",null,null,"40-25","50-48",null,"30-25","40-23","50-48","30-25",null,"50-48","40-45","40-45","40-45","40-50","40-50",null,null,null,null,null,"50-48"]

i need to list the same data as below in javascript

var arrDataSorted = ["40-25","50-48","30-25","40-23","40-45","40-50","40-50"]

need only the common data that replicates also the null to be removed.

What is the best solution to solve this.

Upvotes: 4

Views: 935

Answers (8)

csandreas1
csandreas1

Reputation: 2378

The function can be in a separated file to be reused between multiple pages. Then you can call that function to filter distinct values that are not null.

var arrData = ["40-25",null,null,"40-25","50-48",null,"30-25","40-23","50-48","30-25",null,"50-48","40-45","40-45","40-45","40-50","40-50",null,null,null,null,null,"50-48"];

function fn(value,index,self){
return self.indexOf(value) === index && value;
 }

console.log(arrData.filter(fn));

Upvotes: 0

Mamun
Mamun

Reputation: 68933

You can try using Array.prototype.filter() to remove null values and Set to get the unique values. Finally use the Spread syntax (...) to transform the set result into an array.

Try the following way:

var arrData = ["40-25",null,null,"40-25","50-48",null,"30-25","40-23","50-48","30-25",null,"50-48","40-45","40-45","40-45","40-50","40-50",null,null,null,null,null,"50-48"];

var arrDataSorted = [...new Set(arrData.filter(i => i))];
console.log(arrDataSorted);

Upvotes: 4

Sarun UK
Sarun UK

Reputation: 6736

var arrData = ["40-25",null,null,"40-25","50-48",null,"30-25","40-23","50-48","30-25",null,"50-48","40-45","40-45","40-45","40-50","40-50",null,null,null,null,null,"50-48"]

const output = [];
arrData.forEach(val => {
  if(output.indexOf(val) === -1 && val !== null) {
    output.push(val);
  }
});

console.log(output);

Upvotes: 0

user10876293
user10876293

Reputation:

You could use array built-in reducer method, in the next code i'm starting with an empty array, and i'm only returning the items that are not null and are not already in the array.

const data = arrData.reduce((state, value) => {
   if(value && !state.includes(value)) {
        return [...state, value];
   }

   return state;
}, [])

Upvotes: 0

Vikash Madhow
Vikash Madhow

Reputation: 1344

You can create a set from an array which will automatically remove duplicates:

let arrData = ["40-25",null,null,"40-25","50-48",null,"30-25","40-23","50-48","30-25",null,"50-48","40-45","40-45","40-45","40-50","40-50",null,null,null,null,null,"50-48"];

let set = new Set(arrData);

This will still keep the null, which you can remove with a delete call, and convert back to array with the spread ... operator. The final code will be:

let set = new Set(arrData);
set.delete(null);
let distinctArr = [...set];

Upvotes: 1

Ali
Ali

Reputation: 485

var arrData = ["40-25",null,null,"40-25","50-48",null,"30-25","40-23","50-48","30-25",null,"50-48","40-45","40-45","40-45","40-50","40-50",null,null,null,null,null,"50-48"]
var set = new Set();
      for ( var i = 0 ; i< arrData.length;i++ ) {
           if(arrData[i]!==null) {
                set.add(arrData[i]);
           }
      }
var newArr = [...set]

Upvotes: 0

Levan
Levan

Reputation: 676

Add this function to your code:

function removeCommonValues(arr) {
  let result = [];
  for(let i=0; i < arr.length-1; ++i) {
    if(result.includes(arr[i]) === false && arr[i] !== null)
      result.push(arr[i])
  }
  return result
}

Usage:

removeCommonValues(["40-25",null,null,"40-25","50-48",null,"30-25","40-23","50-48","30-25",null,"50-48","40-45","40-45","40-45","40-50","40-50",null,null,null,null,null,"50-48"]) // Return ["40-25", "50-48", "30-25", "40-23", "40-45", "40-50"]

Upvotes: 0

Ravi
Ravi

Reputation: 2281

add the values into the set if the value is not null and convert it to array.

var arrData = ["40-25",null,null,"40-25","50-48",null,"30-25","40-23","50-48","30-25",null,"50-48","40-45","40-45","40-45","40-50","40-50",null,null,null,null,null,"50-48"];

var setData = new Set();
for(var data of arrData) {
    if(data) {
        setData.add(data);
    }
}

var arrDataSorted = [...setData];
console.log(arrDataSorted);

Upvotes: 0

Related Questions