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

3
  • 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. Commented Jan 2, 2023 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 Commented Jan 2, 2023 at 16:01
  • Clearly you don't, but you should at least be able to find out. See stackoverflow.com/q/7837456/3001761, for example. Do research before posting questions. Commented Jan 2, 2023 at 16:12

4 Answers 4

4

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');
    }
  }
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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>

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.