Home > OS >  How to check array have same values in typescript
How to check array have same values in typescript

Time:01-03

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/app/app.component.ts

CodePudding user response:

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');
    }
  }

CodePudding user response:

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))

CodePudding user response:

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>

CodePudding user response:

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')
  }
}

  • Related