How to make to array data types(object) as strictly equal (===) in JavaScript

0

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

angular javascript operators typescript
2021-11-24 06:52:46
1

0

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
2021-11-24 08:47:02

Thanks@Med Aziz CHETOUI , but In my requirement category Ids are array (i.e public categoryIds: any[] = []; ) and nrtPatchCategory is number(nrtPatchCategory : 30)For this How can I make it as strictly equal(===) can you please help me for this It would be helpful for my requirement
user93

You're welcome, i edit my response and im based on your comment, i hope it's helpful.
Med Aziz CHETOUI

In other languages

This page is in other languages

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................