gpt4 book ai didi

javascript - 我想将不存在的项目推送到已声明的对象中(如果它们尚不存在),则使用 Angular 中的 lodash

转载 作者:行者123 更新时间:2023-12-03 07:45:22 25 4
gpt4 key购买 nike

我是 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
};

我一直知道如何在 Angular 中适本地使用 Lodash,但不确定是否需要使用 Angualar 的 forEach。

我尝试过两种方法。

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

我假设使用_.uniq是最好的方法?任何帮助将不胜感激,我创建了一个 codepen在这里。

TLDR:只需阅读标题

最佳答案

这就是lodash.defaults是:

Assigns own and inherited enumerable properties of source objects to the destination object for all destination properties that resolve to undefined.

lodash.defaults(lessdata, moredata);

关于javascript - 我想将不存在的项目推送到已声明的对象中(如果它们尚不存在),则使用 Angular 中的 lodash,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35236268/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com