javascript - Issue with merging and reordering two arrays of objects -
i'm trying figure out how merge 2 arrays of objects. here's need do:
field
property unique identifier of each object- output needs have objects listed in
originalarray
, including objects inoriginalarray
not exist inlocalstoragearray
- order of
localstoragearray
needs maintained, attention paid previous requirement (order should be:bar
,bee
,foo
,baz
) - output needs contain following property values
localstoragearray
:hidden
,width
(field
give-in, since identifier) - all other properties of
originalarray
need maintained in output
here's wack @ 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 able ordering correct , properties right, 1 issue i'm having when object exists in originalarray
doesn't exist in localstoragearray
, object not included in outputarray
.
any suggestions solution?
here 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 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 understand correctly, want overwrite hidden
, width
properties of objects in originalarray
present in localstoragearray
(2 objects considered same if field
property same)
(function () { var fieldarray = localstoragearray.map(function(e) { return e.field; }); originalarray.foreach(function(originalitem) { var index = fieldarray.indexof(originalitem.field); var localitem; if(index !== -1) { //it's in localstorage, overwrite localitem = localstoragearray[index]; originalitem.hidden = localitem.hidden || originalitem.hidden; originalitem.width = localitem.width || originalitem.width; outputarray.push(originalitem); } })();
instead of creating additional array, can loop through localstoragearray , compare field
property
Comments
Post a Comment