I'm trying to figure out how to merge two arrays of objects. Here's what I need to do:
field
property is the unique identifier of each object- Output needs to only have the objects listed in the
originalArray
, including objects inoriginalArray
that do not exist inlocalStorageArray
- Order of
localStorageArray
needs to be maintained, with attention paid to previous requirement (order should be:bar
,bee
,foo
,baz
) - Output needs to contain the following property values from
localStorageArray
:hidden
andwidth
(field
is a give-in, since its the identifier) - All other properties of
originalArray
need to be maintained in output
Here's my wack at it:
var outputArray = [];
localStorageArray.forEach(function(localItem){
originalArray.forEach(function(originalItem){
if(originalItem.field === localItem.field){
var item = JSON.parse(JSON.stringify(originalItem));
item.hidden = localItem.hidden;
item.width = localItem.width;
outputArray.push(item);
}
});
});
Full JS Fiddle
I was able to get the ordering correct and the properties right, but one issue I'm having is when a object exists in originalArray
that doesn't exist in localStorageArray
, that object is not included in the outputArray
.
Any suggestions to my solution?
Here are my arrays:
var originalArray = [
{field: "foo", hidden: true, sortable: false, template: "<div>#=text#</div>", width: "20px", propA: "a", propB: "b"},
{field: "bee", hidden: true, sortable: false, template: "=#text#", int: 4},
{field: "bar", hidden: false, sortable: false, template: "", width: "20%", propC: "C"},
{field: "baz", hidden: false, sortable: true, template: "<span>#=text#</span>", int: 3}
];
var localStorageArray = [
{field: "bar", hidden: false, sortable: false, width: "100px"},
{field: "foo", hidden: true, sortable: false, template: "<div>#=text#</div>", width: "40px"},
{field: "boo", hidden: true, sortable: true, template: "<div>Boo: #=text#</div>", width: "200px"},
{field: "baz", hidden: true, template: "baz:#=text#", width: "20px"}
];
And here is my desired output:
var desiredArray = [
{field: "bar", hidden: false, sortable: false, template: "", width: "100px", propC: "C"},
{field: "bee", hidden: true, sortable: false, template: "=#text#", int: 4},
{field: "foo", hidden: true, sortable: false, template: "<div>#=text#</div>", width: "40px", propA: "a", propB: "b"},
{field: "baz", hidden: true, sortable: true, template: "<span>#=text#</span>", width: "20px", int: 3}
]
If I understand correctly, you want to overwrite the
hidden
andwidth
properties of those objects inoriginalArray
that are also present inlocalStorageArray
(2 objects are considered the same iffield
property is the same)Instead of creating an additional array, you can also loop through localStorageArray and compare the
field
property