I've been trying to turn the following input:
{
"list": [
{
"name": "light_with_header",
"path": "webapp/master_layouts/sidebar_layouts/",
"type": "Directory",
"data": {
"angular": {
"html": ""
}
}
},
{
"name": "light_with_header",
"path": "webapp/master_layouts/sidebar_layouts/",
"type": "Directory",
"data": {
"angular": {
"script": ""
}
}
},
{
"name": "light_with_header",
"path": "webapp/master_layouts/sidebar_layouts/",
"type": "Directory",
"data": {
"html": {
"html": ""
}
}
},
{
"name": "light_with_header",
"path": "webapp/master_layouts/sidebar_layouts/",
"type": "Directory",
"data": {
"html": {
"script": ""
}
}
},
{
"name": "light_with_header",
"path": "webapp/master_layouts/sidebar_layouts/",
"type": "Directory",
"data": {
"react": {
"script": ""
}
}
},
{
"name": "light_with_header",
"path": "webapp/master_layouts/sidebar_layouts/",
"type": "Directory",
"data": {
"vue": {
"script": ""
}
}
},
{
"name": "light_with_header",
"path": "webapp/master_layouts/sidebar_layouts/",
"type": "Directory",
"data": {
"vue": {
"script": ""
}
}
},
{
"name": "light_with_header_and_icons",
"path": "webapp/master_layouts/sidebar_layouts/",
"type": "Directory",
"data": {
"angular": {
"html": ""
}
}
},
{
"name": "light_with_header_and_icons",
"path": "webapp/master_layouts/sidebar_layouts/",
"type": "Directory",
"data": {
"angular": {
"script": ""
}
}
},
{
"name": "light_with_header_and_icons",
"path": "webapp/master_layouts/sidebar_layouts/",
"type": "Directory",
"data": {
"html": {
"html": ""
}
}
},
{
"name": "light_with_header_and_icons",
"path": "webapp/master_layouts/sidebar_layouts/",
"type": "Directory",
"data": {
"html": {
"script": ""
}
}
},
{
"name": "light_with_header_and_icons",
"path": "webapp/master_layouts/sidebar_layouts/",
"type": "Directory",
"data": {
"react": {
"script": ""
}
}
},
{
"name": "light_with_header_and_icons",
"path": "webapp/master_layouts/sidebar_layouts/",
"type": "Directory",
"data": {
"vue": {
"script": ""
}
}
},
]
}
To the output :
{
list: [
{
name: "light_with_header",
path: "webapp/master_layouts/sidebar_layouts/",
type: "Directory",
data: {
angular: {
html: "",
script: ""
},
html: {
html: "",
script: ""
},
react: {
script: ""
},
vue: {
"script": ""
}
}
},
{
name: "light_with_header_and_icons",
path: "webapp/master_layouts/sidebar_layouts/",
type: "Directory",
data: {
angular: {
html: "",
script: ""
},
html: {
html: "",
script: ""
},
react: {
script: ""
},
vue: {
script: ""
}
},
]
}
Following returns an empty list. It should've merged the objects if the properties are the same:
var anotherList = list.reduce((prev,next)=>{
let newList = mergeArrayObjects(prev,next)
return newList
},[])
function mergeArrayObjects(arr1,arr2){
return arr1.map((item,i)=>{
if(item.name === arr2[i].name){
//merging two objects
return Object.assign({},item,arr2[i])
}
})
}
How to acquire demonstrated output from provided input?