我是lodash的新手,但正如标题所述,我想在已经声明的对象中推送不存在的项目,如果它们不存在的话。那就是我有
var lessdata = {
"id": 1004,
"name": "some event",
"bookmarked": false //not in moredata and I'd like to keep the var as is
};
var moredata = {
"id": 1004,
"name": "some event",
"time": { //from here
"hours": 2,
"minutes": 00,
"currency": "USD"
},
"place": "some place" //to here is new without '"bookmarked": false'
};
我希望将我的结果加载回lessdata
变量并让我的结果看起来像这样
var lessdata = {
"id": 1004,
"name": "some event",
"time": {
"hours": 2,
"minutes": 00,
"currency": "USD"
},
"place": "some place",
"bookmarked": false
};
我知道知道如何使用lodash apprpriatly in angular并且不确定我是否需要使用angualar来获取每个。
我涉及两种方法。
版本1
lessdata= _.uniq(lessdata, function(moredata) {
return moredata;
});
第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
我假设使用_.uniq
是最好的方法吗?任何帮助将不胜感激,我在这里创建了codepen。
TLDR:只需阅读标题
答案 0 :(得分:1)