How to merge object arrays #17753
Replies: 3 comments
-
You should be able to simplify it quite a bit by using a map and item indexes: var config = [
{
name: 'config2'
config: [
{
name: 'thing2'
description: 'Thing 2'
}
{
name: 'thing3'
description: 'Thing 3'
}
]
}
{
name: 'config1'
config: [
{
name: 'thing1'
description: 'Thing 1'
}
]
}
]
var configOverride = [
{
name: 'config2'
config: [
{
name: 'thing4'
description: 'Thing 4'
}
]
}
{
name: 'extraConfig1'
config: [
{
name: 'allTheThings'
description: 'All the things'
}
]
}
]
var overrideMap = union(config, configOverride)
output merged array = map(overrideMap, (item, i) => i == length(overrideMap) - 1 || length(overrideMap) == 0 ? {} : union(item, overrideMap[i + 1])) It results with an array that may have an empty item, but you can wrap the output inside a |
Beta Was this translation helpful? Give feedback.
-
@jachin84 I can see why you would cry 😂 In this case, it can be simpler and more readable for the author. I would wrap the lambda expressions (which can become complex really quickly) in a User-Defined Function. This way, you clearly define what your code does, which can help others (or yourself) in the long run. I would approach it this way: @description('Merges two arrays of objects where the override array takes precedence over items with the same name property')
func mergeArraysByName(baseArray array, overrideArray array) array =>
union(
overrideArray,
filter(baseArray, item => !contains(map(overrideArray, overrideItem => overrideItem.name), item.name))
)
var config = [
{
name: 'config2'
config: [
{
name: 'thing2'
description: 'Thing 2'
}
{
name: 'thing3'
description: 'Thing 3'
}
]
}
{
name: 'config1'
config: [
{
name: 'thing1'
description: 'Thing 1'
}
]
}
]
var configOverride = [
{
name: 'config2'
config: [
{
name: 'thing4'
description: 'Thing 4'
}
]
}
{
name: 'extraConfig1'
config: [
{
name: 'allTheThings'
description: 'All the things'
}
]
}
]
output mergedConfiguration array = mergeArraysByName(config, configOverride) Which will output an array containing 3 objects [
{
"name": "config2",
"config": [
{
"name": "thing4",
"description": "Thing 4"
}
]
},
{
"name": "extraConfig1",
"config": [
{
"name": "allTheThings",
"description": "All the things"
}
]
},
{
"name": "config1",
"config": [
{
"name": "thing1",
"description": "Thing 1"
}
]
}
] |
Beta Was this translation helpful? Give feedback.
-
Thanks for the response @johnlokerse. Your response worked perfectly until I realised some additional requirements. I need to keep the order of the array, and the items are merged not replaced by the config override. var config = [
{
name: 'config2'
otherProperty: 'don\'t delete me!'
config: [
{
name: 'thing2'
description: 'Thing 2'
}
{
name: 'thing3'
description: 'Thing 3'
}
]
}
{
name: 'config1'
config: [
{
name: 'thing1'
description: 'Thing 1'
}
]
}
]
var configOverride = [
{
name: 'config2'
config: [
{
name: 'thing4'
description: 'Thing 4'
}
]
}
{
name: 'extraConfig1'
config: [
{
name: 'allTheThings'
description: 'All the things'
}
]
}
]
var overrideMap = toObject(configOverride ?? [], i => i.name)
var mergedConfig = concat(
map(config, item => union(item, overrideMap[?item.name] ?? {})),
filter(configOverride ?? [], item => !contains(map(config, i => i.name), item.name))
)
output mergedOutput array = mergedConfig
The correct output is: [
{
"name": "config2",
"otherProperty": "don't delete me!",
"config": [
{
"name": "thing4",
"description": "Thing 4"
}
]
},
{
"name": "config1",
"config": [
{
"name": "thing1",
"description": "Thing 1"
}
]
},
{
"name": "extraConfig1",
"config": [
{
"name": "allTheThings",
"description": "All the things"
}
]
}
] |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I am trying to find a simple way to merge two object arrays such that the second array overrides the first. Below is the best example I could come up with however I feel like future me is going to look at this and cry.
Beta Was this translation helpful? Give feedback.
All reactions