【问题标题】:Node JS compliant function to search any object and return the property and value符合 Node JS 的函数,用于搜索任何对象并返回属性和值
【发布时间】:2020-02-03 15:32:06
【问题描述】:

正在寻找一个符合 Node JS 的函数来搜索任何对象的属性和/或值作为字符串,并将所有匹配的属性和/或值作为数组返回。

没有找到任何东西,所以我自己制作了,所以我想我会分享。随意发现缺陷并陈述它们......让我们把它做好!另外,如果有任何现有的工具可以更好地做到这一点,请告诉我。谢谢:)

【问题讨论】:

  • 搜索什么?它究竟返回了什么?有什么要求?
  • 搜索特定字符串作为参数或值或其中之一。已编辑

标签: javascript node.js


【解决方案1】:

小提琴: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;
        }
    }
}

【讨论】:

  • 函数是异步的”为什么?一般来说,这似乎属于Code Review
  • 计划使用它从数据库接收对象,所以我这样做了。我会看一下代码审查。谢谢 VLAZ
  • 太棒了...你能帮我理解你的意思吗?我在一个大型 JSON 解析对象上成功使用了这个函数。它能够搜索结果数组。这也是我将其设为异步的原因......我只是发现它是一种更简单的语法,而不是保存结果然后重用结果等。
  • 每个人似乎都非常关注这会返回一个承诺。我意识到 ATM 不需要外部资源,异步是多余的。我将在我的答案中删除异步,看看是否有更多有用的点需要考虑
  • 太棒了,看看并告诉我我错过了什么...jsfiddle.net/iomatrix/gebz6swm
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多