1

I have some object like this

    const array = 
    {
      "entities": [
        {
          "annexes": [
            {
              "buildingUniqueIds": []
            },
            {
              "buildingUniqueIds": []
            },
            {
              "buildingUniqueIds": []
            },
          ],
          "buildingUniqueIds": []
        }
      ]
}

I need to check if any of those array is empty to return true, is this possible to do in one iteration, here is what I have tried for now, but some does not check array length it always return true even if array is empty :(

  get haveBuildingUniqueIds(): boolean {
    const condition1 = this.array.entities.some(x => x.annexes.some(y => y.buildingUniqueIds.some));
    const condition2 = this.array.entities.some(x => x.buildingUniqueIds.some);
    return condition1 && condition2;
  }

1 Answer 1

1

some is the right method to use, but it's a method, not a flag, so your innermost .some(y => y.buildingUniqueIds.some) will always return true, because a method is a truthy value.

Instead, check length:

const hasEmpties = array.entities.some(({annexes, buildingUniqueIds}) =>
    buildingUniqueIds.length === 0 ||
    annexes.some(({buildingUniqueIds}) => buildingUniqueIds.length === 0)
);

Live Example:

function check(label, array) {
    const hasEmpties = array.entities.some(({annexes, buildingUniqueIds}) =>
        buildingUniqueIds.length === 0 ||
        annexes.some(({buildingUniqueIds}) => buildingUniqueIds.length === 0)
    );
    console.log(label, "=>", hasEmpties);
}

check("all empty", {
    "entities": [
        {
            "annexes": [
                {
                    "buildingUniqueIds": []
                },
                {
                    "buildingUniqueIds": []
                },
                {
                    "buildingUniqueIds": []
                },
            ],
            "buildingUniqueIds": []
        }
    ]
});

check("none empty", {
    "entities": [
        {
            "annexes": [
                {
                    "buildingUniqueIds": [{}]
                },
                {
                    "buildingUniqueIds": [{}]
                },
                {
                    "buildingUniqueIds": [{}]
                },
            ],
            "buildingUniqueIds": [{}]
        }
    ]
});

check("entities.buildingUniqueIds empty", {
    "entities": [
        {
            "annexes": [
                {
                    "buildingUniqueIds": [{}]
                },
                {
                    "buildingUniqueIds": [{}]
                },
                {
                    "buildingUniqueIds": [{}]
                },
            ],
            "buildingUniqueIds": []
        }
    ]
});

check("entities.annexes.buildingUniqueIds[1] empty", {
    "entities": [
        {
            "annexes": [
                {
                    "buildingUniqueIds": [{}]
                },
                {
                    "buildingUniqueIds": []
                },
                {
                    "buildingUniqueIds": [{}]
                },
            ],
            "buildingUniqueIds": [{}]
        }
    ]
});

Side note: If you like, you can use !buildingUniqueIds.length rather than buildingUniqueIds.length === 0.

Or without destructuring if you prefer:

const hasEmpties = array.entities.some(entity =>
    entity.buildingUniqueIds.length === 0 ||
    entity.annexes.some(annex => annex.buildingUniqueIds.length === 0)
);

Live Example:

function check(label, array) {
    const hasEmpties = array.entities.some(entity =>
        entity.buildingUniqueIds.length === 0 ||
        entity.annexes.some(annex => annex.buildingUniqueIds.length === 0)
    );
    console.log(label, "=>", hasEmpties);
}

check("all empty", {
    "entities": [
        {
            "annexes": [
                {
                    "buildingUniqueIds": []
                },
                {
                    "buildingUniqueIds": []
                },
                {
                    "buildingUniqueIds": []
                },
            ],
            "buildingUniqueIds": []
        }
    ]
});

check("none empty", {
    "entities": [
        {
            "annexes": [
                {
                    "buildingUniqueIds": [{}]
                },
                {
                    "buildingUniqueIds": [{}]
                },
                {
                    "buildingUniqueIds": [{}]
                },
            ],
            "buildingUniqueIds": [{}]
        }
    ]
});

check("entities.buildingUniqueIds empty", {
    "entities": [
        {
            "annexes": [
                {
                    "buildingUniqueIds": [{}]
                },
                {
                    "buildingUniqueIds": [{}]
                },
                {
                    "buildingUniqueIds": [{}]
                },
            ],
            "buildingUniqueIds": []
        }
    ]
});

check("entities.annexes.buildingUniqueIds[1] empty", {
    "entities": [
        {
            "annexes": [
                {
                    "buildingUniqueIds": [{}]
                },
                {
                    "buildingUniqueIds": []
                },
                {
                    "buildingUniqueIds": [{}]
                },
            ],
            "buildingUniqueIds": [{}]
        }
    ]
});

Sign up to request clarification or add additional context in comments.

5 Comments

@MiomirDancevic - Sorry, missed that that wasn't just another entry in annexes, but it's an easy fix. I've updated the answer.
'buildingUniqueIds' is already declared in the upper scope.
@MiomirDancevic - That's okay, it just gets shadowed. But you don't have to use destructuring if you don't want to.
Can you please help me sonar cloud does not allow that
@MiomirDancevic - I've updated the answer, but I do recommend reading up on parameter destructuring so you can do it yourself next time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.