in Education by
I was unable to find a solution that is identical with the issue I am trying to solve. If is a duplicate kindly provide a link to a solution. Using vanilla Javascript, merge array of objects that has a common value. I have a JSON file and it will create the following javascript object (I am unable to change the original structure). Notice that each object will have different nested name and nested value. However the common value is the name[0] var data = [ { name: [ 'Data 1', // common value '1 Jan, 2019', // same value therefore not overwrite/merge 'hotfix dec' ], value: [ 'hotfix1.fsfix', 'hotfix1.txt' ] }, { name: [ 'Data 1', // common value '1 Jan, 2019' // same value therefore not overwrite/merge ], 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 desire output will be merging the nested object that has the same name[0]. var data = [ { name: [ 'Data 1', // common value '1 Jan, 2019', // same value therefore not overwrite/merge 'hotfix dec' ], value: [ 'data1.fsfix', 'data1.txt', 'data1.jar' // This was added after the merge ] }, { name: [ 'Data 2', '1 Feb, 2019' ], value: [ 'data2.fsfix', 'data2.jar' // This was added after the merge ] }, { name: [ 'Data 3', '1 Mar, 2018' ], value: 'data3.fsfix' } ] Using this new merged structure, I would create a function to loop each array set. Thanks in advance JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
You could key the data by the first name entry using a Map. Then populate the data into the value property within the corresponding Map value, also collect all the extra values in the name arrays (beyond the first two entries), and finally extract the Map values. This works with linear time complexity: var data = [{name: ['Data 1', '1 Jan, 2019', 'hotfix dec'],value: ['hotfix1.fsfix','hotfix1.txt']},{name: ['Data 1', '1 Jan, 2019'],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'}]; const map = new Map(data.map(o => [o.name[0], { name: o.name.slice(0,2), value: [] }])); data.forEach(o => map.get(o.name[0]).value.push(...[].concat(o.value))); data.forEach(o => map.get(o.name[0]).name.push(...o.name.slice(2))); const result = Array.from(map.values(), o => o.value.length === 1 ? { ...o, value: o.value[0] } : o); console.log(result); Run code snippetExpand snippet The callback function that is passed to Array.from is a map-function. It is only needed to turn value arrays with only one string into that string only. If this is not required, you can omit that callback and just do Array.from(map.values()).

Related questions

0 votes
    I was unable to find a solution that is identical with the issue I am trying to solve. If is a ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    i have an array of objects in [ { "country": "USA", "payment": [ { "paymentType": "Visa" ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 6, 2022 in Education by JackTerrance
0 votes
    This is my object. values : { title : 'this is title ..', translate : { en : 'some texts ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 3, 2022 in Education by JackTerrance
0 votes
    This is my object. values : { title : 'this is title ..', translate : { en : 'some texts ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 3, 2022 in Education by JackTerrance
0 votes
    I store all of the Google Maps marker objects in a single array. Right now I am trying to set up ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 30, 2022 in Education by JackTerrance
0 votes
    I try to merge some object into one array I did output by q = [["99","99","99"],["9" ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 1, 2022 in Education by JackTerrance
0 votes
    My question is a bit complicated and I wasted whole day without any good effect I get from one place ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 17, 2022 in Education by JackTerrance
0 votes
    I am trying to merge 2 data frames by using the id and then trying to save the result in the JSON file. | |id|a|b ... |3|15|3, 12|6, 15 Select the correct answer from above options...
asked Jan 11, 2022 in Education by JackTerrance
0 votes
    I have this JSON tree view that represents a menu : var menus = [ { label : "1", items: [ ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 3, 2022 in Education by JackTerrance
0 votes
    object looks like as below { "id":1, "image":"path/to/image", "employee_data":, } again ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 13, 2022 in Education by JackTerrance
0 votes
    object looks like as below { "id":1, "image":"path/to/image", "employee_data":, } again ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I have two different objects with different columns in DB. Since I need to show both of those objects ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    I have two different objects with different columns in DB. Since I need to show both of those objects ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 14, 2022 in Education by JackTerrance
0 votes
    Is there a better way than this to splice an array into another array in javascript var string = ' ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 10, 2022 in Education by JackTerrance
0 votes
    how to push an element into an array in javascript?...
asked Dec 10, 2020 in Technology by JackTerrance
...