-1

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

user93
  • 39
  • 1
  • 4
  • "*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 ===)*" that means you do not have arrays on both sides. https://dorey.github.io/JavaScript-Equality-Table/ [Which equals operator (== vs ===) should be used in JavaScript comparisons?](https://stackoverflow.com/q/359494) – VLAZ Nov 24 '21 at 06:55
  • No, both sides array(object datatypes ) only – user93 Nov 24 '21 at 07:02
  • No, that is impossible. Objects are compared by identity in every single case, so `==` and `===` are exactly the same. If you say one returns `true` the other `false`, then you don't have objects on either side. Most likely you're comparing something like `"30" == [30]` which would be `true` because of implicit conversion. But not with strict equality because the types don't match. – VLAZ Nov 24 '21 at 07:05
  • Does this help: https://stackoverflow.com/questions/27030/comparing-arrays-of-objects-in-javascript? – Darshna Rekha Nov 24 '21 at 07:15

1 Answers1

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
Med Aziz CHETOUI
  • 158
  • 1
  • 3
  • 15
  • 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 Nov 24 '21 at 08:24
  • You're welcome, i edit my response and im based on your comment, i hope it's helpful. – Med Aziz CHETOUI Nov 24 '21 at 08:49