我有两个对象数组,表示具有标签和值的电子邮件地址:
var original = [
{
label: 'private',
value: 'private@johndoe.com'
},
{
label: 'work',
value: 'work@johndoe.com'
}
];
var update = [
{
label: 'private',
value: 'me@johndoe.com'
},
{
label: 'school',
value: 'schhol@johndoe.com'
}
];
Run Code Online (Sandbox Code Playgroud)
现在我想按label
字段比较和合并两个数组,以便结果如下所示:
var result = [
{
label: 'private',
value: 'me@johndoe.com'
},
{
label: 'work',
value: 'work@johndoe.com'
},
{
label: 'school',
value: 'schol@johndoe.com'
}
]
Run Code Online (Sandbox Code Playgroud)
我怎么能这样做,例如使用lodash?
有人可以在JavaScript中提供有关模块加载器和模块捆绑器的一些信息吗?
我有两个对象数组,其中包含具有标签和实际地址对象的地址:
var originalAddresses = [
{
label: 'home',
address: { city: 'London', zipCode: '12345' }
},
{
label: 'work',
address: { city: 'New York', zipCode: '54321' }
}
];
var updatedAddresses = [
{
label: 'home',
address: { city: 'London (Central)', country: 'UK' }
},
{
label: 'spain',
address: { city: 'Madrid', zipCode: '55555' }
}
];
Run Code Online (Sandbox Code Playgroud)
现在我想合并这些数组label
并比较地址的各个属性,并仅合并实际存在的新地址的属性.所以结果应该是这样的:
var result = [
{
label: 'home',
address: { city: 'London (Central)', zipCode: '12345', country: 'UK' }
},
{
label: …
Run Code Online (Sandbox Code Playgroud) 我有一个看起来像这样的猫鼬模型:
var ProjectSchema = new Schema({
name: { type: String, required: true },
tags: [{ type: String, required: true }]
});
Run Code Online (Sandbox Code Playgroud)
我希望项目至少有一个标签是必需的.但是当我保存没有标签数组的新项目时,mongoose不会抛出错误:
var project = new Project({'name': 'Some name'});
project.save(function(err, result) {
// No error here...
});
Run Code Online (Sandbox Code Playgroud)
我在这里错过了什么?如何指定所需的数组?