skip to Main Content

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 playerTypes 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


  1. I think it’s straight forward. Not sure what issue you are fetching with Array.some() method.

     allPLayerTypesAndTextInfo.some((item) => {
        return !validPlayerTypes.has(item.playerType);
     });
    

    If this solution doesn’t work. Please let me know on which section you are having issue.

    Login or Signup to reply.
  2. You can try every :

    let allPlayerTypesAndTextInfoValidValues = [
      { playerType: 1, text: "some text A" },
      { playerType: 1, text: "some text B" },
      { playerType: 1, text: "some text C" },
      { playerType: 2, text: "some text D" },
      { playerType: 1, text: "some text E" },
      { playerType: 1, text: "some text F" },
      { playerType: 2, text: "some text G" },
      { playerType: 2, text: "some text H" }
    ];
    let allPlayerTypesAndTextInfo = [
      { playerType: 1, text: "some text A" },
      { playerType: 1, text: "some text B" },
      { playerType: 1, text: "some text C" },
      { playerType: 2, text: "some text D" },
      { playerType: 1, text: "some text E" },
      { playerType: 3, text: "some text F" },
      { playerType: 3, text: "some text G" },
      { playerType: 3, text: "some text H" }
    ];
    let requiredValues = new Set(allPlayerTypesAndTextInfoValidValues.map(p => p.playerType));
    let containOnlyValidValues = allPlayerTypesAndTextInfo.every(p => requiredValues.has(p.playerType));
    console.log(containOnlyValidValues);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search