数组对象是对象数组的子集
Is array objects subset of array of objects
我正在尝试编写一个函数来检查对象数组是否是另一个对象数组的子集,我希望它是 return 布尔值。
这是我目前的情况:
var arr1 = [
{ answer: "dispersed", question: "where_is_your_flare_located" },
{ answer: "continuous", question: "what_frequnecy_of_measurement_do_i_require_to_assess_methane_emissions" }
];
var arr2 = [
{ question: 'where_is_your_flare_located', answer: 'dispersed' },
{ question: 'what_frequnecy_of_measurement_do_i_require_to_assess_methane_emissions', answer: 'periodic' },
{ question: 'how_long_is_the_flare_expected_to_be_in_operation', answer: 'n_a' },
{ question: 'am_i_managing_an_existing_flare_or_planning_to_install_a_new_one', answer: 'n_a' },
{ question: 'do_i_have_access_to_assistance_gases', answer: '' },
{ question: 'do_i_have_access_to_purge_gas', answer: '' },
];
var compare = arr2.filter(function(o) {
var resultQuestion = o.question;
var resultAnswer = o.question;
return arr1.some(function(i) {
var filterQuestion = i.question;
var filterAnswer = i.question;
return ((filterQuestion === resultQuestion) && (resultAnswer === filterAnswer));
});
});
console.log(compare)
我不明白我做错了什么。
我将不胜感激任何帮助或解释。
这只是查看 arr1
中的 every
元素是否存在于 arr2
中的情况
var arr1 = [
{ answer: "dispersed", question: "where_is_your_flare_located" },
{ answer: "continuous", question: "what_frequnecy_of_measurement_do_i_require_to_assess_methane_emissions" }
// comment the above and uncomment below to return true
//{ answer: "periodic", question: "what_frequnecy_of_measurement_do_i_require_to_assess_methane_emissions" }
];
var arr2 = [
{ question: 'where_is_your_flare_located', answer: 'dispersed' },
{ question: 'what_frequnecy_of_measurement_do_i_require_to_assess_methane_emissions', answer: 'periodic' },
{ question: 'how_long_is_the_flare_expected_to_be_in_operation', answer: 'n_a' },
{ question: 'am_i_managing_an_existing_flare_or_planning_to_install_a_new_one', answer: 'n_a' },
{ question: 'do_i_have_access_to_assistance_gases', answer: '' },
{ question: 'do_i_have_access_to_purge_gas', answer: '' },
];
const isSubset = arr1.every(a1 => arr2.find(a2 => a1.answer == a2.answer && a1.question == a2.question));
console.log(isSubset);
我正在尝试编写一个函数来检查对象数组是否是另一个对象数组的子集,我希望它是 return 布尔值。
这是我目前的情况:
var arr1 = [
{ answer: "dispersed", question: "where_is_your_flare_located" },
{ answer: "continuous", question: "what_frequnecy_of_measurement_do_i_require_to_assess_methane_emissions" }
];
var arr2 = [
{ question: 'where_is_your_flare_located', answer: 'dispersed' },
{ question: 'what_frequnecy_of_measurement_do_i_require_to_assess_methane_emissions', answer: 'periodic' },
{ question: 'how_long_is_the_flare_expected_to_be_in_operation', answer: 'n_a' },
{ question: 'am_i_managing_an_existing_flare_or_planning_to_install_a_new_one', answer: 'n_a' },
{ question: 'do_i_have_access_to_assistance_gases', answer: '' },
{ question: 'do_i_have_access_to_purge_gas', answer: '' },
];
var compare = arr2.filter(function(o) {
var resultQuestion = o.question;
var resultAnswer = o.question;
return arr1.some(function(i) {
var filterQuestion = i.question;
var filterAnswer = i.question;
return ((filterQuestion === resultQuestion) && (resultAnswer === filterAnswer));
});
});
console.log(compare)
我不明白我做错了什么。 我将不胜感激任何帮助或解释。
这只是查看 arr1
中的 every
元素是否存在于 arr2
var arr1 = [
{ answer: "dispersed", question: "where_is_your_flare_located" },
{ answer: "continuous", question: "what_frequnecy_of_measurement_do_i_require_to_assess_methane_emissions" }
// comment the above and uncomment below to return true
//{ answer: "periodic", question: "what_frequnecy_of_measurement_do_i_require_to_assess_methane_emissions" }
];
var arr2 = [
{ question: 'where_is_your_flare_located', answer: 'dispersed' },
{ question: 'what_frequnecy_of_measurement_do_i_require_to_assess_methane_emissions', answer: 'periodic' },
{ question: 'how_long_is_the_flare_expected_to_be_in_operation', answer: 'n_a' },
{ question: 'am_i_managing_an_existing_flare_or_planning_to_install_a_new_one', answer: 'n_a' },
{ question: 'do_i_have_access_to_assistance_gases', answer: '' },
{ question: 'do_i_have_access_to_purge_gas', answer: '' },
];
const isSubset = arr1.every(a1 => arr2.find(a2 => a1.answer == a2.answer && a1.question == a2.question));
console.log(isSubset);