3

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

EMahan K
  • 417
  • 1
  • 19
  • 2
    This is basic JS, nothing TS-specific (and certainly nothing to do with Angular). Your code makes no _attempt_ to check whether the values are in the other array, just copies it and asserts on whether or not it's truth-y. – jonrsharpe Jan 02 '23 at 15:56
  • @jonrsharpe: I do not know how to check all the values are same in the array. If you know please edit my stackblitz – EMahan K Jan 02 '23 at 16:01
  • Clearly you don't, but you should at least be able to _find out_. See https://stackoverflow.com/q/7837456/3001761, for example. Do research before posting questions. – jonrsharpe Jan 02 '23 at 16:12

4 Answers4

3

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');
    }
  }
Andriu1510
  • 409
  • 1
  • 6
1

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))
R4ncid
  • 6,944
  • 1
  • 4
  • 18
0

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')
  }
}
Wimanicesir
  • 4,606
  • 2
  • 11
  • 30
0

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>
Selaka Nanayakkara
  • 3,296
  • 1
  • 22
  • 42