I am trying to find present value in array using include()
method but it's showing wrong result.
I had below code for the same.
I have "percentageValue" array with following values
console.log("this.percentageArray before checking percentValue", this.percentageArray); //below is answer for the same
// logs
[
{ percValue: 8, lastPerc: 0 },
{ percValue: 27, lastPerc: 0 },
{ percValue: 29, lastPerc: 27 },
{ percValue: 30, lastPerc: 27 },
{ percValue: 35, lastPerc: 27 },
{ percValue: 44, lastPerc: 27 },
{ percValue: 60, lastPerc: 27 },
{ percValue: 35, lastPerc: 27 },
{ percValue: 85, lastPerc: 60 },
}
I've written code for checking percentvalue 85
console.log("this.percentValue and this.lastPercentage from flag loop", this.percentValue, this.lastPercentage)// getting this answer(this.percentValue and this.lastPercentage from flag loop 85 60)
this.percentValuefromFlag = this.percentageArray.includes(this.percentValue, this.lastPercentage);
console.log("percentvalue and lastPercentage present or not", this.percentValuefromFlag)//for checking result value
but I'm getting false as result
CodePudding user response:
.includes
takes one required argument, and one optional argument. The first one is the element you want to match. The second one is the index from which you want to start the search.
So, if you want to match an object in your array, do it like this:
this.percentageArray.includes({percValue: this.percValue, lastPerc: this.lastPercentage})
However, this won't work because JS will compare values by reference (except for scalars and strings), not by comparing the values inside the object.
You should use .some()
instead:
this.percentageArray.some(element => element.percValue === this.percValue && element.lastPerc === this.lastPercentage)
CodePudding user response:
consider using some
instead
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some
[
{ percValue: 8, lastPerc: 0 },
{ percValue: 27, lastPerc: 0 },
{ percValue: 29, lastPerc: 27 },
{ percValue: 30, lastPerc: 27 },
{ percValue: 35, lastPerc: 27 },
{ percValue: 44, lastPerc: 27 },
{ percValue: 60, lastPerc: 27 },
{ percValue: 35, lastPerc: 27 },
{ percValue: 85, lastPerc: 60 },
].some(({percValue, lastPerc }) => percValue === 85 && lastPerc === 60) // true
or alternatively filter
, if you need to access the value:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
CodePudding user response:
.includes()
is useful when you're dealing with primitives types. In this case, you're dealing with objects, which are considered non-primitive type or complex type.
There are several ways to deal with list of objects, Javascript provides some methods like find()
or findIndex()
.
You could use .find()
for example:
this.percentValuefromFlag = this.percentageArray.find((elem) => {
return elem.percValue === this.percentValue && elem.lastPerc === this.lastPercentage
})
if (this.percentValuefromFlag) {
console.log(this.percentValuefromFlag)
}