1

I have an object like this,

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

i want to Generate the Multiple objecs By using the INCLUDE Property in the Main object, can anybody help me on this

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

2.  
{
    "parent_id":"",
    "id":"DISTRIBUTOR1",
    "permission":"Granted",
    "country_code":"United States"
}
Abdelaziz Mokhnache
  • 4,269
  • 3
  • 25
  • 35
Jeevan
  • 756
  • 13
  • 39

3 Answers3

1

You could split the included countries and map new objects.

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; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

You can try something like this:

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)

Note: I have used Object.assign to copy but it is not supported by all browsers. Please refer browser compatibility before using. You can also refer following post for alternatives: What is the most efficient way to deep clone an object in JavaScript?

Community
  • 1
  • 1
Rajesh
  • 24,354
  • 5
  • 48
  • 79
0

This is another approach to solve current problem what could also be answer for possible new question "How could you make it more complex"

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; }
Tolgahan Albayrak
  • 3,118
  • 1
  • 25
  • 28