我有这个对象数组。如何在checkArray
函数中传递键列表以验证一个对象中的键?
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: 'milmar@gmail.com',
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: 'milmar@gmail.com',
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'
}
];
function isKeyInObject(obj, key) {
var res = Object.keys(obj).some(v => v == key);
return res;
}
const checkArray = (test, key) => {
let valid = true;
test.map(item => {
if (!isKeyInObject(item, key)) valid = false;
});
return valid;
};
console.log(checkArray(test, 'VAT'));
我如何在此处传递整个密钥列表以进行验证?现在,我只能通过一把钥匙。我正在寻找类似checkArray(test, 'VAT','destination','insured_tin');
答案 0 :(得分:1)
您可以使用Array的every功能轻松实现它
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: 'milmar@gmail.com',
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: 'milmar@gmail.com',
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) => {
return test.every(item => keys.every(key => item.hasOwnProperty(key)));
};
console.log(checkArray(test, ['VAT', 'currency_code']));
console.log(checkArray(test, ['VAT', 'unknown']));
答案 1 :(得分:0)
您可以简单地在JavaScript中使用hasOwnProperty()
对象
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: 'milmar@gmail.com',
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: 'milmar@gmail.com',
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','intermediary_name']));
console.log(checkArray(test,['VAT','intermediary']));