javascript - I'd like to push non-existing items inside an already declared object if they don't already exist using lodash in angular -
i'm new lodash title states 'i'd push non-existing items inside declared object if don't exist' if have
var lessdata = { "id": 1004, "name": "some event", "bookmarked": false //not in moredata , i'd keep var }; var moredata = { "id": 1004, "name": "some event", "time": { //from here "hours": 2, "minutes": 00, "currency": "usd" }, "place": "some place" //to here new without '"bookmarked": false' };
i'd have result loaded lessdata
variable , have result
var lessdata = { "id": 1004, "name": "some event", "time": { "hours": 2, "minutes": 00, "currency": "usd" }, "place": "some place", "bookmarked": false };
i stuck knowing know use lodash apprpriatly in angular , wasnt sure if need use angualar's foreach or not.
i've dabbled 2 approaches.
version 1
lessdata= _.uniq(lessdata, function(moredata) { return moredata; });
version 2
angular.foreach(lessdata, function(lkey, lvalue) { console.log("[-]lessdata---lkey: " + lkey + ", lvalue: " + lvalue) angular.foreach(moredata, function(mkey, mvalue) { console.log("[+]moredata---mkey: " + mkey + ", mvalue: " + mvalue) lessdata=_.uniq(lessdata, function(moredata) { return moredata; }); }) }) $scope.event = lessdata
im assuming using _.uniq
best approach? appreciated , created codepen here.
tldr: read title
that's lodash.defaults
does:
assigns own , inherited enumerable properties of source objects destination object destination properties resolve
undefined
.
lodash.defaults(lessdata, moredata);
Comments
Post a Comment