Home > front end >  how to make to array data types(object) as strictly equal (===) in JavaScript
how to make to array data types(object) as strictly equal (===) in JavaScript

Time:11-25

In my application I have to make two array datatypes(one is any[] and other is number[]) as equal using strictly equal.

my code is:

.component.ts

 if (categoryIds ===  PhysicalPackageConst.nrtPatchCategory){
               this.materialTypes = PhysicalPackageConst.nrtPatchMaterialType;
categoryIds = [];
            
                  }

In the above if condition it is showing as false if I make it as ===(if I use == it is showing the data(true) but not for ===)

package.constant.ts

export const PhysicalPackageConst = {
nrtGumCategory : [29],
    nrtPatchCategory : [30]

So I want to make it as true for the above condition in strictly condition Can anyone help me on this

CodePudding user response:

Strict Equality Comparison (===) ("strict equality", "identity", "triple equals") : Strict equality compares two values for equality. Neither value is implicitly converted to some other value before being compared. If the values have different types, the values are considered unequal. If the values have the same type, are not numbers, and have the same value, they're considered equal. Otherwise to compare value.

var num = 0;
var str = '0';

console.log(num === str); // false

Abstract Equality Comparison (==) ("loose equality", "double equals") : The behavior for performing loose equality using == is as follows. Loose equality compares two values for equality after converting both values to a common type. After conversions (one or both sides may undergo conversions), the final equality comparison is performed exactly as === performs it.

var num = 0;
var str = '0';

console.log(num === str); // true

Equality comparisons and sameness

For your problem, it's logic to get those result, because you need to cast value of array :any[] to number and make strict compare.

let categoryIds: any[] = [];
let nrtPatchCategory: number = 30;
// browse categoryIds arrays (you can use any other method like for ...)
categoryIds.forEach(categoryId => {
  if (Number(categoryId) === nrtPatchCategory) {
    ...
  }
});

Note: For more detail of forEach() Array.prototype.forEach()

Exemple:

console.log(2 === Number('3')); // false
console.log(3 === Number('3')); // true
  • Related