【问题标题】:How to generate the multiple objects using another object in javascript如何在javascript中使用另一个对象生成多个对象
【发布时间】:2017-01-31 10:30:38
【问题描述】:

我有一个这样的对象,

{
    "Distrubutor":"DISTRIBUTOR1",
    "INCLUDE":"INDIA,United States",
    "EXCLUDE":"KARNATAKA-INDIA,CHENNAI-TAMILNADU-INDIA",
    "PARENT-ID":""
}

我想通过使用 Main 对象中的 INCLUDE 属性来生成多个对象,有人可以帮我解决这个问题吗

{
    "parent_id":"",
    "id":"DISTRIBUTOR1",
    "permission":"Granted",
    "country_code":"INDIA"
}

2.  
{
    "parent_id":"",
    "id":"DISTRIBUTOR1",
    "permission":"Granted",
    "country_code":"United States"
}

【问题讨论】:

    标签: javascript json node.js lodash


    【解决方案1】:

    您可以拆分包含的国家并映射新对象。

    var data = {"Distrubutor":"DISTRIBUTOR1","INCLUDE":"INDIA,United States","EXCLUDE":"KARNATAKA-INDIA,CHENNAI-TAMILNADU-INDIA","PARENT-ID":""},
        include = data.INCLUDE.split(',').map(function (a) {
            return {
                parent_id: "",
                id: data.Distrubutor,
                permission: "Granted",
                country_code: a                
            };
        });
    
    console.log(include);
    .as-console-wrapper { max-height: 100% !important; top: 0; }

    【讨论】:

      【解决方案2】:

      你可以试试这样的:

      var obj = {"Distrubutor":"DISTRIBUTOR1","INCLUDE":"INDIA,United States","EXCLUDE":"KARNATAKA-INDIA,CHENNAI-TAMILNADU-INDIA","PARENT-ID":""}
      
      // Keys that does not need to be in returned object
      var ignoreKeys = ["EXCLUDE", "INCLUDE"];
      
      var tmp = Object.assign({}, obj)
      
      // Delete all keys that are not required
      ignoreKeys.forEach(function(key){ delete tmp[key] })
      
      var result = obj.INCLUDE.split(',').map(function(country){
        return Object.assign({}, tmp, {"country_code": country, "permission": "GRANTED"})
      })
      
      console.log(result)

      注意:我使用Object.assign复制,但并非所有浏览器都支持。使用前请参考browser compatibility。您也可以参考以下帖子以获取替代方案:What is the most efficient way to deep clone an object in JavaScript?

      【讨论】:

        【解决方案3】:

        这是解决当前问题的另一种方法,也可以回答可能的新问题“你如何使它更复杂”

        class PropertyAdapter {
          *populate(target, option){}
        }
        
        class DistrubutorAdapter extends PropertyAdapter {
          *populate(target, option){
            target.id = option;
            yield target;
          }
        }
        
        class ParentIdAdapter extends PropertyAdapter {
          *populate(target, option){
            target.parent_id = option;
            yield target;
          }  
        }
        
        class IncludeAdapter extends PropertyAdapter {
          *populate(target, option){
            option = option.split(/,/g).map(x=>x.trim()).filter(x=>!!x.length);
            for(let v of option){
              let obj = Instantinator.copy(target);
              obj.country_code = v;
              yield obj;
            }
          }  
        }
        
        class Instantinator {
          static create(){
            return {permission: 'Granted'};
          }
          
          static copy(src){
            return Object.assign({}, src);
          }
        }
        
        const ADAPTERS = new Map();
        
        class ObjectFactory {
          static register(prop, adapter){
            ADAPTERS.set(prop, adapter);
          }
          
          static create(options){
            const pairs = Object.keys(options||{})
              .filter(key => ADAPTERS.has(key))
              .map(key=> ({value: options[key], adapter: ADAPTERS.get(key)}));
            let result = [Instantinator.create()];
            for(let pair of pairs){
              result = result.reduce((prev, cur) => prev.concat([...pair.adapter.populate(cur, pair.value)]), []);
            }
            return result;
          }
        }
        
        ObjectFactory.register('Distrubutor', new DistrubutorAdapter());
        ObjectFactory.register('PARENT-ID', new ParentIdAdapter());
        ObjectFactory.register('INCLUDE', new IncludeAdapter());
        
        
        var options = {
            "Distrubutor":"DISTRIBUTOR1",
            "INCLUDE":"INDIA,United States",
            "EXCLUDE":"KARNATAKA-INDIA,CHENNAI-TAMILNADU-INDIA",
            "PARENT-ID":""
        };
        
        console.log(ObjectFactory.create(options));
        .as-console-wrapper { max-height: 100% !important; top: 0; }

        【讨论】:

          猜你喜欢
          • 2020-05-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-03
          • 1970-01-01
          • 2012-12-05
          相关资源
          最近更新 更多