javascript - 符合 Node JS 的函数,用于搜索任何对象并返回属性和值
问题描述
正在寻找一个符合 Node JS 的函数来搜索任何对象的属性和/或值作为字符串,并将所有匹配的属性和/或值作为数组返回。
没有找到任何东西,所以我自己制作了,所以我想我会分享。随意发现缺陷并陈述它们......让我们把它做好!另外,如果有任何现有的工具可以更好地做到这一点,请告诉我。谢谢 :)
解决方案
小提琴: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;
}
}
}
推荐阅读
- java - NTLM 和 Java Apache httpclient 4.5.6 的多线程问题
- c - 函数引用参数:指针与原型只是样式?
- sql - SQL Server - 如何使部分重复的行从原始行继承值
- .net - 提取 Sustainsys SAML 2.0 断言
- c++ - 基类类型向量中的派生结构
- android - 以edittext为条件检查firebase值或获取用户输入
- asp.net-mvc - 在 asp.net mvc 中使用quartz.net 通过电子邮件发送定期发票
- javascript - 无需用户即可运行并将数据存储在数据库中的 Node.js 应用程序
- reactjs - TypeError:将循环结构转换为 JSON Babel-cli
- machine-learning - 通过 PV-DBOW 学习的 doc 向量是否等同于 doc 中包含的词向量的平均值/总和?