Skip to content

Instantly share code, notes, and snippets.

@abel-masila
Last active June 25, 2018 18:37
Show Gist options
  • Save abel-masila/b182639de8faf475eebc15cddb4c0c56 to your computer and use it in GitHub Desktop.
Save abel-masila/b182639de8faf475eebc15cddb4c0c56 to your computer and use it in GitHub Desktop.
var test = [
{
date_issued: '2018-05-25',
transaction_type: 'IMPORT',
policy_number: '061/100/001052/2018',
open_cover: false,
endorsement_number: '',
icc: 'ICC-A',
insured_name: 'SAYONA STEEL LTD',
insured_email: '[email protected]',
insured_tin: '103 286 883',
intermediary_name: 'MILMAR',
intermediary_tin: '100-714-078',
goods_category: 'Mineral Products',
country_of_origin: 'HK',
destination: 'Mwanza Region',
conveyance: 'SEA',
financier: '',
currency_code: 'USD',
exchange_rate_used: '2262.37',
sum_insured: '23302411',
premium: '33935.55',
VAT: '5176.609322',
unit_of_measure: 'UNITS',
quantity: '800',
cover_start_date: '2018-05-15'
},
{
date_issued: '2018-05-25',
transaction_type: 'IMPORT',
policy_number: '061/100/001051/2018',
open_cover: false,
endorsement_number: '',
icc: 'ICC-A',
insured_name: 'URHOME COMPANY LTD',
insured_email: '[email protected]',
insured_tin: '132-209-898',
intermediary_name: 'MILMAR',
intermediary_tin: '100-714-078',
goods_category: 'Base Metals And Articles Of Base Metal',
country_of_origin: 'CN',
destination: 'Dar es Salaam Region',
conveyance: 'SEA',
financier: '',
currency_code: 'USD',
exchange_rate_used: '2262.37',
sum_insured: '54941202.98',
premium: '58821.62',
VAT: '8972.789492',
unit_of_measure: 'UNITS',
quantity: '2792',
cover_start_date: '2018-05-15'
}
];
const checkArray = (test, keys) => {
let valid = true;
test.map(object => {
keys.map(key => {
if (!object.hasOwnProperty(key)) {
valid = false;
}
});
});
return valid;
};
console.log(checkArray(test, ['VAT', 'currency_code', 'quantity']));
@geofmureithi-zz
Copy link

/**
 * @name checkArray
 * @description Checks if all objects in the array have all needed keys
 * @param {Array} test
 * @param {Array} keys
 * @returns bool
 *
 **/
const checkArray = (test, keys) => {
	return test.filter(object => {
		return keys.map(key => object.hasOwnProperty(key)).indexOf(false) == -1
	}).length === test.length;
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment