小提琴:http://jsfiddle.net/iomatrix/gebz6swm/1/
用法:
try{
let results = contains( object, string1, {match: "VALUE", cs: true, exact: true } )
results = contains( results, string2, { match: "ANY", cs: true, exact: true } )
}
catch(err){
console.log( "Catching errors is important :): " + err.stack ) );
}
函数是递归的,可以深入研究。它将返回所有匹配 {property: value} 对的数组和它的父级。此时它只有字符串匹配,但可以扩展为包含其他数据类型进行匹配。
结果可以链接(见上文)以过滤到特定所需的组件。
function contains( o, s, options={}, result=[]){ //OPTION CAN BE "PROPERTY", "VALUE", or default("ANY")
//Returns array of results
//options etc: {match: "ANY"/"PROPERTY"/"VALUE", cs: true/false, exact: true/false}
let match = options.match ? options.match : "ANY";
let cs = options.cs ? options.cs : false;
let exact = options.exact ? options.exact : false;
let match_any = (match.toUpperCase() == "ANY") ? true : false;
let match_property = (match.toUpperCase() == "PROPERTY") ? true : false;
let match_value = (match.toUpperCase() == "VALUE") ? true : false;
//IS OBJECT?
if (typeof o === 'object'){
if(Array.isArray(o)){
//Iterate Array, IF Value is Object, recurse to contains
for(const e of o){
if (typeof e === 'object'){
contains(e,s,options, result);
}
else{
if (cs){
if(exact){
if( ( match_any || match_value ) && String(e) == String(s) ){
obj["parent"] = o;
result.push(e);
}
}
else{
if( ( match_any || match_value ) && String(e).includes(String(s)) ){
obj["parent"] = o;
result.push(e);
}
}
}
else{
if(exact){
if( ( match_any || match_value ) && String(e).toLowerCase() == String(s).toLowerCase() ){
obj["parent"] = o;
result.push(e);
}
}
else{
if( ( match_any || match_value ) && String(e).toLowerCase().includes( String(s).toLowerCase() ) ){
obj["parent"] = o;
result.push(e);
}
}
}
}
}
return result;
}
else{
//Iterate Object, IF Value is Object, recurse to contains
for (const p in o){
if(typeof o[p] === 'object'){
if (cs){
if(exact){
if( (match_any || match_property) && String(p) == String(s) ){
let obj = {}
obj["parent"] = o;
obj[p] = o[p];
result.push(obj);
}
}
else{
if( (match_any || match_property) && String(p).includes(String(s))){
let obj = {}
obj["parent"] = o;
obj[p] = o[p];
result.push(obj);
}
}
contains(o[p],s, options, result);
}
else{
if(exact){
if( (match_any || match_property) && String(p).toLowerCase() == String(s).toLowerCase() ){
let obj = {}
obj["parent"] = o;
obj[p] = o[p];
result.push(obj);
}
}
else{
if( (match_any || match_property) && String(p).toLowerCase().includes( String(s).toLowerCase() )){
let obj = {}
obj["parent"] = o;
obj[p] = o[p];
result.push(obj);
}
}
contains(o[p],s, options, result);
}
}
else{
if (cs){
if(exact){
if( (match_any || match_property) && String(p) == String(s) ){
let obj = {}
obj["parent"] = o;
obj[p] = o[p];
result.push(obj);
}
if( (match_any || match_value) && String(o[p]) == String(s) ){
let obj = {}
obj["parent"] = o;
obj[p] = o[p];
result.push(obj);
}
}
else{
if( (match_any || match_property) && String(p).includes(String(s)) ){
let obj = {}
obj["parent"] = o;
obj[p] = o[p];
result.push(obj);
}
if( (match_any || match_value) && String(o[p]).includes(String(s))){
let obj = {}
obj["parent"] = o;
obj[p] = o[p];
result.push(obj);
}
}
}
else{
if(exact){
if( (match_any || match_property) && String(p).toLowerCase() == String(s).toLowerCase() ){
let obj = {}
obj["parent"] = o;
obj[p] = o[p];
result.push(obj);
}
if( (match_any || match_value) && String(o[p]).toLowerCase() == String(s).toLowerCase() ){
let obj = {}
obj["parent"] = o;
obj[p] = o[p];
result.push(obj);
}
}
else{
if( (match_any || match_property) && String(p).toLowerCase().includes( String(s).toLowerCase() )){
let obj = {}
obj["parent"] = o;
obj[p] = o[p];
result.push(obj);
}
if( (match_any || match_value) && String(o[p]).toLowerCase().includes( String(s).toLowerCase() )){
let obj = {}
obj["parent"] = o;
obj[p] = o[p];
result.push(obj);
}
}
}
}
}
return result;
}
}
else{
if (cs){
if(exact){
if( (match_any || match_value) && String(o) == String(s) ){
obj["parent"] = o;
result.push(o);
}
}
else{
if( (match_any || match_value) && String(o).includes(String(s))){
obj["parent"] = o;
result.push(o);
}
}
return result;
}
else{
if(exact){
if( (match_any || match_value) && String(o).toLowerCase() == String(s).toLowerCase() ){
obj["parent"] = o;
result.push(o);
}
}
else{
if( (match_any || match_value) && String(o).toLowerCase().includes( String(s).toLowerCase() )){
obj["parent"] = o;
result.push(o);
}
}
return result;
}
}
}