I have an array with type of PlayerTextInfo.
Array of type PlayerTextInfo = [{playerType:1, text:""}]
allPLayerTypesAndTextInfo = [
0: {playerType: 1, text: "some text A"}
1: {playerType: 1, text: "some text B"}
2: {playerType: 1, text: "some text C"}
3: {playerType: 2, text: "some text D"}
4: {playerType: 1, text: "some text E"}
5: {playerType: 1, text: "some text F"}
6: {playerType: 2, text: "some text G"}
7: {playerType: 2, text: "some text H"}
]
I want to get all the playerType
s and create a new numerical Set with no duplicate values.
const validPlayerTypes= new Set([]);
allPLayerTypesAndTextInfo.forEach((x) =>validPlayerTypes
.add(x.playerType));
// validPlayerTypes ==> {1,2}
If there is at least one object in the allPLayerTypesAndTextInfo with playerType which is not included in the Set then I want to return false.
For example: Last three values have playerType = 3 which is not included in validPlayerTypes Set. All objects of allPLayerTypesAndTextInfo must have playerType either 1 or 2.
allPLayerTypesAndTextInfo = [
0: {playerType: 1, text: "some text A"}
1: {playerType: 1, text: "some text B"}
2: {playerType: 1, text: "some text C"}
3: {playerType: 2, text: "some text D"}
4: {playerType: 1, text: "some text E"}
5: {playerType: 3, text: "some text F"}
6: {playerType: 3, text: "some text G"}
7: {playerType: 3, text: "some text H"}
]
I have tried Array.prototype.every
and Array.prototype.some
.
How can I do the above?
2
Answers
I think it’s straight forward. Not sure what issue you are fetching with Array.some() method.
If this solution doesn’t work. Please let me know on which section you are having issue.
You can try every :