EMahan K
EMahan K

Reputation: 467

How to check array have same values in typescript

Trying to find array have same values or not in typescript but not working. So, How to findout. If anyone knows please help to find the solution.

app.component.ts:

  arr1 = ['1256','1256','1256'];
  arr2 = ['1256','8259','1256'];
  newArr=[];

 checkVal(val){
 val.forEach(x=>{ 
   this.newArr.push(x); 
 });

 if(this.newArr){
  alert("All the values are same in the array")
 }else{
  alert("No Diffent values are there in this array")
  } 
 }

 checkValApply1(){
  this.checkVal(this.arr1)
 }

 checkValApply2(){
  this.checkVal(this.arr2)
 }

Demo: https://stackblitz.com/edit/angular-ivy-9xyxxm?file=src%2Fapp%2Fapp.component.ts

Upvotes: 3

Views: 2306

Answers (4)

Wimanicesir
Wimanicesir

Reputation: 5121

You can do this simply removing the duplicates and checking if one numbers remains.

let arr1 = ['1256','1256','1256'];
let arr2 = ['1256','8259','1256'];

checkArray(arr1)
checkArray(arr2)

function checkArray(array) {
  // Remove duplicates 
  let filtered = [...new Set(array)]

  // If only one numbers remains, they are all the same :) 
  if (filtered.length === 1) {
    console.log('All values are the same')
    } else {
    console.log('There are different values')
  }
}

Upvotes: 0

R4ncid
R4ncid

Reputation: 7129

you can use Array.reduce or you can use Set like this

 const arr1 = ['1256','1256','1256'];
 const arr2 = ['1256','8259','1256'];
 
 
 const allTheSameElementReduce = data => !!data.reduce((res, el) => res === el?res: false )
 
 const allTheSameWithSet = data => new Set(data).size === 1
 
 console.log(arr1,allTheSameElementReduce(arr1))
console.log(arr2,allTheSameElementReduce(arr2))
 
console.log(arr1,allTheSameWithSet(arr1))
console.log(arr2,allTheSameWithSet(arr2))

Upvotes: 1

SELA
SELA

Reputation: 6803

Here's slightly a different answer assuming you want to compare both these arrays together with each element in the other array if they are equal then proceed with some task and if not then proceed with another task.

Please find the working stackblitz sample here.

Please find the working code below:

app.component.ts :

import { Component, VERSION } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})
export class AppComponent {
  name = 'Angular ' + VERSION.major;

  arr1 = ['1256', '1256', '1256'];
  arr2 = ['1256', '8259', '1256'];

  checkValues() {
    if (this.equalsCheck(this.arr1, this.arr2)) {
      alert('The arrays have the same elements.');
    } else {
      alert('The arrays have different elements.');
    }
  }

  equalsCheck = (a, b) =>
    a.length === b.length && a.every((v, i) => v === b[i]);
}

app.component.html :

<button (click)="checkValues()">Add same </button>

Upvotes: 0

Andriu1510
Andriu1510

Reputation: 409

We can use the every function, since we check that the first value of the array is equal to the following, and it can be applied as follows:

checkVal(val) {
    const firstValue = val[0];
    const isSame = val.every((x) => x === firstValue);

    if (isSame) {
      alert('All the values are same in the array');
    } else {
      alert('No Diffent values are there in this array');
    }
  }

Upvotes: 3

Related Questions