Check the response of an API for correctness
Towards better performance and code organization

The list of required fields is better defined as const at top level:

const requiredFields = ["code", "name", "pricings"];


There are 2 negative outcomes that can be combined and checked at once:
1) if the response is not of Array type, 2) if it's empty array. The Consolidate conditional expression technique is applied.

if (!Array.isArray(resp) || !resp.length) {
    return false;
}


Instead of getting list of keys for each object in response array with var keys = Object.keys(obj) - the more efficient and faster way is to just apply obj.hasOwnProperty(el) to check if an object obj has required field el as a key.
All the rest "movements" in the initial approach are redundant and eliminated.



The final optimized approach:



function checkResponseApi(resp, mustHave) {     
    if (!Array.isArray(resp) || !resp.length) {
     return false;
    }
    return resp.every(obj =&gt; {
        return mustHave.every(el =&gt; obj.hasOwnProperty(el))
    });
}

let arr = [
    {"code": "X", "name": "Product A", "pricings": []},
    {"code": "Y", "name": "Product B", "pricings": []},
    {"code": "Z", "name": "Product C", "pricings": []}
];
const requiredFields = ["code", "name", "pricings"];


if (checkResponseApi(arr, requiredFields)) {
    console.log('Ok')
} else {
    console.log('Bad response')
}