check is Object is in array by ptoperty(-ies)
check is Object is in array of objects by one or several (are not necessarily all) properties-values of object. Returns array of object(-s) if tested array contains object with desired pairs of property-value or returns FALSE if no matches found
by Yury
JavaScript
/* check is Object is in array of objects by one or several (are not necessarily all) properties-values of object. Returns array of object(-s) if tested array contains object with desired pairs of property-value or returns FALSE if no matches found */
function checkObjPropInArray(tObj, tArr) {
var obj = tObj || null;
var arr = tArr || null;
var isContArr = [];
if (!obj || typeof obj !== 'object' || !arr || !arr.length){return false;}
for (var i = 0; i < arr.length; i++) {
var ica = [];
for (var prop in obj) {
if (arr[i].hasOwnProperty(prop) ){
if (arr[i][prop] == obj[prop]){
ica.push(true);
} else {
ica.push(false);
}
} else{
ica.push(false);
continue;
}
}
if (!(ica.indexOf(false) >= 0)){
isContArr.push(arr[i]);
}
}
if (isContArr.length){
return isContArr;
} else {
return false;
}
}
/* USING: passing parameters 1st - dummy object, 2nd - tested array */
var checkVoice = AppBg.checkObjPropInArray({'lang': 'en-GB', 'gender': "female"}, voicesArr);
/* var checkVoice may contain array of matched object(-s) or FALSE if nothing found */
/* NOTICE: array "voicesArr" may contain similar objects with many properties than dummy object {'lang': 'en-GB', 'gender': "female"} - not two but a lot more, like
{
extensionId: "neajdppkdcdipfabeoofebfddakdcjhd",
gender: "male",
lang: "en-GB",
remote: true,
voiceName: "Google UK English Male"
}
*/