How to define if there's an error in object with multiple array

const data = {
      test1: [
        {error: ''},
        {error: ''},
      ],
      test2: [
        {error: ''},
        {error: 'theres an error in this field'},
        {error: 'theres an error here'},
      ],
      test3: [
        {error: ''},
        {error: 'theres an error'},
        {error: 'theres an error here'},
      ]
    };

how to check if the array has an error. which it should display an error message.

for example I have a <div> with condition and the data return true once there's only one true it will display the error message.

here's my code

 const test1 = data.test1.some(x => x.error !== ''); 
    const test2 = data.test2.some(x => x.error !== ''); 
    const test3 = data.test3.some(x => x.error !== ''); 
    
        <div *ngIf="test1 || test2 || test3">
            DISPLAY ERROR
        </div>

I'm not sure it this is the correct way.


You can use Object.values to get an array of the values of the properties of the object and use some, and within the some callback you can return the result of your some calls on the value if it's an array:

const hasError = Object.values(data).some(value => Array.isArray(value) && value.some(x => x.error));

If you know all the properties are these testX properties, you can drop the Array.isArray part.

Live Example:

const dataWithError = {
    test1: [
        {error: ''},
        {error: ''},
      ],
    test2: [
        {error: ''},
        {error: 'theres an error in this field'},
        {error: 'theres an error here'},
    ],
    test3: [
        {error: ''},
        {error: 'theres an error'},
        {error: 'theres an error here'},
    ]
};
const dataWithoutError = {
    test1: [
        {error: ''},
        {error: ''},
      ],
    test2: [
        {error: ''},
        {error: ''},
        {error: ''},
    ],
    test3: [
        {error: ''},
        {error: ''},
        {error: ''},
    ]
};

const dataWithErrorHasError = Object.values(dataWithError).some(
    value => Array.isArray(value) && value.some(x => x.error)
);
console.log(`dataWithError:`, dataWithErrorHasError);

const dataWithoutErrorHasError = Object.values(dataWithoutError).some(
    value => Array.isArray(value) && value.some(x => x.error)
);
console.log(`dataWithoutError:`, dataWithoutErrorHasError);

Or for just specific properties from data, build the array directly:

const hasError = [data.test1, data.test2, data.test3].some(test => test.some(x => x.error));

Live Example:

const dataWithError = {
    test1: [
        {error: ''},
        {error: ''},
      ],
    test2: [
        {error: ''},
        {error: 'theres an error in this field'},
        {error: 'theres an error here'},
    ],
    test3: [
        {error: ''},
        {error: 'theres an error'},
        {error: 'theres an error here'},
    ]
};
const dataWithoutError = {
    test1: [
        {error: ''},
        {error: ''},
      ],
    test2: [
        {error: ''},
        {error: ''},
        {error: ''},
    ],
    test3: [
        {error: ''},
        {error: ''},
        {error: ''},
    ]
};

const dataWithErrorHasError = [dataWithError.test1, dataWithError.test2, dataWithError.test3].some(test => test.some(x => x.error));
console.log(`dataWithError:`, dataWithErrorHasError);

const dataWithoutErrorHasError = [dataWithoutError.test1, dataWithoutError.test2, dataWithoutError.test3].some(test => test.some(x => x.error));
console.log(`dataWithoutError:`, dataWithoutErrorHasError);