将一个对象的值和属性合并到另一个对象中
Merge values and properties from one object into another
我有两个独立的对象:
对象 1
var obj1 = {
features: {
"1234": {
"price": 0
},
"1235": {
"price": 0
}
}
};
对象 2
var obj2 = {
features: {
"1234": {
"price": 1000,
"property": "val"
},
"1235": {
"price": 500,
"property": "val"
}
}
};
我想将 对象 1 的价格 属性 设置为 对象 2[= 的价格 属性 的值37=] 键(1234、1235 等)在两个对象中匹配。
此外,我想知道如何从 对象 2 向 对象 1 添加新的 属性。
最后我希望 对象 3 看起来像:
var obj3 = {
features: {
"1234": {
"price": 1000,
"property": "val"
},
"1235": {
"price": 500,
"property": "val"
}
}
};
我试过以下方法,但一定有更好的方法吗?
var obj3 = {};
for (key in obj1) {
if (obj1.hasOwnProperty(key)) {
var val = obj1[key];
if (typeof val === 'object') {
for (obj1_key in val) {
if (val.hasOwnProperty(obj1_key)) {
if (obj1_key === 'price') {
// set value to value of obj2 price property
}
// add a new property to object #3
obj3[obj1_key]['property'] = 'value_of_object_2_property';
}
}
}
// add to new object #3
obj3[key] = val;
}
}
我知道有一些库可以帮助处理诸如下划线之类的事情,所以如果您有关于如何使用下划线或 lodash 实现此目的的建议,我愿意接受任何建议。
你可以用 lodash 来做。在这两个解决方案中,您可以使用 _.pluck
函数从 obj2
中提取任何字段。
解决方案 1,通过 obj1
个字段循环:
var obj3 = { features: {} };
_(obj1.features)
.keys()
.each(function(id) {
var price = ob2.prices[id];
if (price) {
obj3.features[id] = _.pick(price, ['price', 'property']);
}
})
.value();
解决方案 2,使用 reduce
函数:
var obj3 = _.reduce(ob1.feature, function(result, value, id) {
var price = ob2.prices[id];
if (price) {
result.features[id] = _.pick(price, ['price', 'property']);
}
return result;
}, { features: {}});
使用_.merge
This method is like _.assign except that it recursively merges own and inherited enumerable string keyed properties of source objects into the destination object. Source properties that resolve to undefined are skipped if a destination value exists. Array and plain object properties are merged recursively.Other objects and value types are overridden by assignment. Source objects are applied from left to right. Subsequent sources overwrite property assignments of previous sources.
var obj1 = {
features: {
"1234": {
"price": 0
},
"1235": {
"price": 0
}
}
};
var obj2 = {
features: {
"1234": {
"price": 1000,
"property": "val"
},
"1235": {
"price": 500,
"property": "val"
}
}
};
var obj3 = {};
_.merge(obj3,obj1,obj2);
document.getElementById('result').innerHTML = JSON.stringify(obj3,null,2);
console.log(obj3);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.10.0/lodash.min.js"></script>
<pre id='result'></pre>
我有两个独立的对象:
对象 1
var obj1 = {
features: {
"1234": {
"price": 0
},
"1235": {
"price": 0
}
}
};
对象 2
var obj2 = {
features: {
"1234": {
"price": 1000,
"property": "val"
},
"1235": {
"price": 500,
"property": "val"
}
}
};
我想将 对象 1 的价格 属性 设置为 对象 2[= 的价格 属性 的值37=] 键(1234、1235 等)在两个对象中匹配。
此外,我想知道如何从 对象 2 向 对象 1 添加新的 属性。
最后我希望 对象 3 看起来像:
var obj3 = {
features: {
"1234": {
"price": 1000,
"property": "val"
},
"1235": {
"price": 500,
"property": "val"
}
}
};
我试过以下方法,但一定有更好的方法吗?
var obj3 = {};
for (key in obj1) {
if (obj1.hasOwnProperty(key)) {
var val = obj1[key];
if (typeof val === 'object') {
for (obj1_key in val) {
if (val.hasOwnProperty(obj1_key)) {
if (obj1_key === 'price') {
// set value to value of obj2 price property
}
// add a new property to object #3
obj3[obj1_key]['property'] = 'value_of_object_2_property';
}
}
}
// add to new object #3
obj3[key] = val;
}
}
我知道有一些库可以帮助处理诸如下划线之类的事情,所以如果您有关于如何使用下划线或 lodash 实现此目的的建议,我愿意接受任何建议。
你可以用 lodash 来做。在这两个解决方案中,您可以使用 _.pluck
函数从 obj2
中提取任何字段。
解决方案 1,通过 obj1
个字段循环:
var obj3 = { features: {} };
_(obj1.features)
.keys()
.each(function(id) {
var price = ob2.prices[id];
if (price) {
obj3.features[id] = _.pick(price, ['price', 'property']);
}
})
.value();
解决方案 2,使用 reduce
函数:
var obj3 = _.reduce(ob1.feature, function(result, value, id) {
var price = ob2.prices[id];
if (price) {
result.features[id] = _.pick(price, ['price', 'property']);
}
return result;
}, { features: {}});
使用_.merge
This method is like _.assign except that it recursively merges own and inherited enumerable string keyed properties of source objects into the destination object. Source properties that resolve to undefined are skipped if a destination value exists. Array and plain object properties are merged recursively.Other objects and value types are overridden by assignment. Source objects are applied from left to right. Subsequent sources overwrite property assignments of previous sources.
var obj1 = {
features: {
"1234": {
"price": 0
},
"1235": {
"price": 0
}
}
};
var obj2 = {
features: {
"1234": {
"price": 1000,
"property": "val"
},
"1235": {
"price": 500,
"property": "val"
}
}
};
var obj3 = {};
_.merge(obj3,obj1,obj2);
document.getElementById('result').innerHTML = JSON.stringify(obj3,null,2);
console.log(obj3);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.10.0/lodash.min.js"></script>
<pre id='result'></pre>