I have been searching for a solution to address my specific issue but have not found an exact match. If there is a similar question, please provide a link to the solution.
I am looking to merge an array of objects that share a common value using vanilla Javascript. I have a JSON file that will generate the following javascript object, and I am unable to alter the original structure. Each object will have different nested names and values, but the common value is found in name[0].
var data = [
{
name: [
'Data 1', // common value
'1 Jan, 2019', // same value, should not be overwritten/merged
'hotfix dec'
],
value: [
'hotfix1.fsfix',
'hotfix1.txt'
]
},
{
name: [
'Data 1', // common value
'1 Jan, 2019' // same value, should not be overwritten/merged
],
value: 'data1.jar'
},
{
name: [
'Data 2',
'1 Feb, 2019'
],
value: 'data2.fsfix'
},
{
name: [
'Data 2',
'1 Feb, 2019'
],
value: 'data2.jar'
},
{
name: [
'Data 3',
'1 Mar, 2018'
],
value: 'data3.fsfix'
}
]
The desired output is to merge nested objects that share the same name[0].
var data = [
{
name: [
'Data 1', // common value
'1 Jan, 2019', // same value, should not be overwritten/merged
'hotfix dec'
],
value: [
'data1.fsfix',
'data1.txt',
'data1.jar' // Added after the merge
]
},
{
name: [
'Data 2',
'1 Feb, 2019'
],
value: [
'data2.fsfix',
'data2.jar' // Added after the merge
]
},
{
name: [
'Data 3',
'1 Mar, 2018'
],
value: 'data3.fsfix'
}
]
With this new merged structure, I plan to create a function to iterate through each array set. Thank you in advance for your help.