0

I have an object array with duplicate key / value pairs. In this instance, each system has multiple sources. I would like to manipulate it's structure and add system.Title as an object for a single source so that each source has multiple systems.

How can I do this with .map() or .reduce()?

I have supplied the input and desired output below.

//INPUT
var doctypes = [{
  "Source": [{
    "Title": "Anchor (WI)"
  }, {
    "Title": "Lafayette Savings Bank"
  }, {
    "Title": "United Bancorp"
  }, {
    "Title": "Old National"
  }],
  "System": {
    "Title": "Nautilus"
  },
  "Title": "Deposit Documents",
}, {
  "Source": [{
    "Title": "Founders"
  }],
  "System": {
    "Title": "SharePoint"
  },
  "Title": "Deposit Documents",
}, {
  "Source": [{
    "Title": "Anchor (MN)"
  }],
  "System": {
    "Title": "OmniView"
  },
  "Title": "Deposit Documents",
}, {
  "Source": [{
    "Title": "Old National"
  }, {
    "Title": "Anchor (WI)"
  }, {
    "Title": "Lafayette Savings Bank"
  }, {
    "Title": "United Bancorp"
  }],
  "System": {
    "Title": "CSS (Aperio)"
  },
  "Title": "Deposit Documents",
}]


// DESIRED OUTPUT
var doctypes = [{
  Source: 'Anchor (MN)',
  System: [{
    Title: 'OmniView'
  }]
}, {
  Source: 'Anchor (WI)',
  System: [{
    Title: 'CSS (Aperio)'
  }, {
    Title: 'Nautilus'
  }]
}, {
  Source: 'Founders',
  System: [{
    Title: 'SharePoint'
  }]
}, {
  Source: 'Lafayette Savings Bank',
  System: [{
    Title: 'CSS (Aperio)'
  }, {
    Title: 'Nautilus'
  }]
}, {
  Source: 'Old National',
  System: [{
    Title: 'CSS (Aperio)'
  }, {
    Title: 'Nautilus'
  }]
}, {
  Source: 'United Bancorp',
  System: [{
    Title: 'CSS (Aperio)'
  }, {
    Title: 'Nautilus'
  }]
}]

console.log(doctypes)

1 Answer 1

2

Looks like you want to re-index the array from System to Source. I like to use dictionaries, or objects, or hash maps to pivot from on index to another. There are many other approaches to this, but I looped through doctypes and created a new object with the Source.Title and the keys and added any Systems I found while looping to that key'd object. Then converted the object back into an array. See code below.

//INPUT
var doctypes = [{
  "Source": [{
    "Title": "Anchor (WI)"
  }, {
    "Title": "Lafayette Savings Bank"
  }, {
    "Title": "United Bancorp"
  }, {
    "Title": "Old National"
  }],
  "System": {
    "Title": "Nautilus"
  },
  "Title": "Deposit Documents",
}, {
  "Source": [{
    "Title": "Founders"
  }],
  "System": {
    "Title": "SharePoint"
  },
  "Title": "Deposit Documents",
}, {
  "Source": [{
    "Title": "Anchor (MN)"
  }],
  "System": {
    "Title": "OmniView"
  },
  "Title": "Deposit Documents",
}, {
  "Source": [{
    "Title": "Old National"
  }, {
    "Title": "Anchor (WI)"
  }, {
    "Title": "Lafayette Savings Bank"
  }, {
    "Title": "United Bancorp"
  }],
  "System": {
    "Title": "CSS (Aperio)"
  },
  "Title": "Deposit Documents",
}];

var source_dict = [];
doctypes.forEach((dc)=>{
	dc.Source.forEach((src)=>{
  	if(!source_dict.hasOwnProperty(src.Title)){
    	source_dict[src.Title] = {
      	Source:src.Title,
      	System:[]
      }
    }
    source_dict[src.Title].System.push({Title:dc.System.Title});
  })
})
console.log(source_dict);
var doctypes_out = [];
for(var o in source_dict){
  doctypes_out.push(source_dict[o]);
}
console.log(doctypes_out);

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

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.