David
David

Reputation: 4271

How to split javascript object with the same attribute into array

How to split javascript object with the same attribute.

Here I have one object

var fields = [
    {"Country" : "USA", "key": "Country", Value :"USA"},
    {"State" : "SC", "key": "State", Value :"SC"},
    {"District" : ["DIST1","DIST2"], "key": "District", Value :["DIST1","DIST2"]}];

I wanted to store this in object like this way

var fields = [
{"Country" : "USA","State" :"SC","District" : "DIST1"},
{"Country" : "USA","State" :"SC","District" : "DIST2"}]

For that what I am trying here is

var myObj = {};

for(var i = 0; i<fields.length; i++){
        myObj[fields[i].key] = fields[i].Value
}

and o/p i am getting is

{
    Country: "USA"
    District:["DIST1","DIST2"]
    State: "SC"
}

Any solution for expected o/p. How can split "District here"

Upvotes: 1

Views: 123

Answers (2)

Jurshsmith
Jurshsmith

Reputation: 182

It a data study problem. Using ES6, try this:

let fields = [
    {"Country" : "USA", "key": "Country", Value :"USA"},
    {"State" : "SC", "key": "State", Value :"SC"},
    {"District" : ["DIST1","DIST2"], "key": "District", Value :["DIST1","DIST2"]}];

// create enums for your fields
const FieldsEnums = Object.freeze({
  COUNTRY: "Country",
  STATE: "State",
  DISTRICT: "District"
});

// returns the new field object based on the district provided
const getFieldStructure = (fields, district) => fields.reduce((newFields, field) => {
    const { COUNTRY, STATE, DISTRICT } = FieldsEnums;

    switch(field["key"]){
      case COUNTRY: {
        newFields[COUNTRY] = field.Value;
        break;
      }
      case STATE: {
        newFields[STATE] = field.Value;
        break;
      }
      case DISTRICT: {
        newFields[DISTRICT] = district;
        break;
      }
    }

    return newFields;

  }, {});

// finds the districts in old structure and generates the new objects based on each district
const newFields = fields
          .find(field => field["key"] === FieldsEnums.DISTRICT)
          .Value
          .map(district => getFieldStructure(fields, district));

console.log('new Fields', newFields)

Upvotes: 1

Greedo
Greedo

Reputation: 3549

This function will do the job: it will split the object arrays into multiple copied objects. Objectes will be deep-copied, so no issue from cross-modifications.

var fields = [
    {"Country" : "USA", "key": "Country", Value :"USA"},
    {"State" : "SC", "key": "State", Value :"SC"},
    {"District" : ["DIST1","DIST2"], "key": "District", Value :["DIST1","DIST2"]}];

var myObj = {};

for(var i = 0; i<fields.length; i++){
        myObj[fields[i].key] = fields[i].Value
}

var myObjList = [];
var listFlag = false;
Object.keys(myObj).forEach(function(key){
  if(Array.isArray(myObj[key])){
    listFlag = true;
    myObj[key].forEach(function(el){
      var objCopy = JSON.parse(JSON.stringify(myObj))
      objCopy[key] = el;
      myObjList.push(objCopy);
    });
  };
});

if(!listFlag){
  myObjList.push(myObj);
}
    
console.log(myObjList)

Upvotes: 1

Related Questions