skip to Main Content

I’ve below code snippet

const arr = [
    {
        "name": "Attestation Component 1",
        "values": [
            {
                "component": "Attestation Component 1"
            },
            {
                "component": "Attestation Component 1"
            },
            {
                "component": "Attestation Component 1",
            }
        ]
    },
    {
        "name": "Attestation Component 2",
        "values": [
            {
                "id": "10005884",
                "url": "https://www.msn.com",
                "bfaId": "G44.5.3.1N/A",
                "component": "Attestation Component 2"
            },
            {
                "id": "10005883",
                "url": "https://www.hotmail.com",
                "bfaId": "G44.5.3.2N/A",
                "component": "Attestation Component 2"
            }
        ]
    },
    {
        "name": "Attestation Component 3",
        "values": [
            {
                "id": "10005882",
                "url": "https://www.rediffmail.com",
                "bfaId": "G44.5.3.3N/A",
                "component": "Attestation Component 3"
            }
        ]
    }
]

const bool = arr.map(group => group.values.every(val => val.id));
console.log(bool);

I’ve three object with name Attestation Component 1, Attestation Component 2 ,Attestation Component 3. I’m getting the expected output as false, true, true. What’s the reason for this? I want to add the property to the existing array of object as something isInvalid: true/false below name

Expected O/P
isInvalid: true/false

3

Answers


  1. That is because your algorithm is incorrect. The every method will check if all the objects have an id, but that is not what you want right ?

    So try this instead

    const bool = arr.map(group => group.values.some(val => val.id)).filter(bool => !bool).toString();
    
    Login or Signup to reply.
  2. Instead of every(), you should use some().

    const bool = arr.map(group => group.values.some(val => val.id)).filter(bool => !bool).toString();
    

    every() method is used to check whether all the elements of the array satisfy the given condition or not. The Array. some() method is used to check whether at least one of the elements of the array satisfies the given condition or not.

    Login or Signup to reply.
  3. Check if there is at least one object in the arr array that has a value with the property named id using the .some() method.

    const arr = [
        {
            "name": "Attestation Component 1",
            "values": [
                {
                    "component": "Attestation Component 1"
                },
                {
                 // "id": "10005886",
                    "url": "www.outlook.com",
                    "component": "Attestation Component 1"
                },
                {
                 // "id": "10005885",
                    "url": "www.aol.com",
                    "component": "Attestation Component 1",
                }
            ]
        },
        {
            "name": "Attestation Component 2",
            "values": [
                {
                //  "id": "10005884",
                    "url": "https://www.msn.com",
                    "bfaId": "G44.5.3.1N/A",
                    "component": "Attestation Component 2"
                },
                {
                //  "id": "10005883",
                    "url": "https://www.hotmail.com",
                    "bfaId": "G44.5.3.2N/A",
                    "component": "Attestation Component 2"
                }
            ]
        },
        {
            "name": "Attestation Component 3",
            "values": [
                {
                //  "id": "10005882",
                    "url": "https://www.rediffmail.com",
                    "bfaId": "G44.5.3.3N/A",
                    "component": "Attestation Component 3"
                }
            ]
        }
    ]
    
    const bool = arr.some(obj => obj.values.some(val => 'id' in val));
    console.log(bool);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search