1

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"

3
  • 4
    the object that you want to get eventually is not valid in js Commented Jun 30, 2020 at 11:52
  • @zb22 not eventually, it is invalid Commented Jun 30, 2020 at 12:05
  • What is the expected output? Commented Jun 30, 2020 at 12:06

2 Answers 2

1

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)

Sign up to request clarification or add additional context in comments.

Comments

1

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.