[英]How do I merge two objects such that keys from one map to values of another, without the extra keys in the second, using latest Javascript?
Object A :对象A :
let form= {
first_name: "",
last_name: "",
avatar: ""
}
Object B对象 B
let profile {
first_name: "John",
last_name: "Doe",
avatar: "https://image.cdn.com",
....100 other fields
}
Basically I should end up with:基本上我应该结束:
newObject: {
first_name: "John",
last_name: "Doe",
avatar: "https://image.cdn.com"
}
Without those extra items in the second object.在第二个对象中没有那些额外的项目。
I tried this but it still returns the extra keys I don't want:我试过了,但它仍然返回我不想要的额外键:
let profile= {
first_name: "John",
last_name: "Doe",
avatar: "https://image.cdn.com",
key1: 1,
key2: 2,
key: 3,
}
let form= {
first_name: "",
last_name: "",
avatar: ""
}
console.log(_.merge(form, profile));
You can use Object.keys()
and Array.reduce()
to iterate over the object您可以使用
Object.keys()
和Array.reduce()
来迭代对象
let form = { first_name: "", last_name: "", avatar: "" } let profile = { first_name: "John", last_name: "Doe", avatar: "https://image.cdn.com", dummy: 'Dummy' } let newObject = Object.keys(form).reduce((acc, key) => { acc[key] = profile[key]; return acc }, {}) console.log(newObject)
UPDATE:更新:
With lodash
you can do it as followings:使用
lodash
您可以执行以下操作:
let form = { first_name: "", last_name: "", avatar: "" } let profile = { first_name: "John", last_name: "Doe", avatar: "https://image.cdn.com", dummy: 'Dummy' } let intersection = _.intersection(Object.keys(form), Object.keys(profile)) console.log(intersection); intersection.forEach(key => form[key] = profile[key]) console.log(form);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>
You can grab the keys from one object using Object.keys()
, then .map()
these keys to an object, which uses the key from the map as the key for the object and the value from the other object for that given key as the value for the object.您可以使用
Object.keys()
从一个对象中获取键,然后.map()
这些键到一个对象,该对象使用映射中的键作为对象的键,并使用另一个对象的值作为给定键作为对象的值。 Then use Object.assign()
to build an object from this array of objects:然后使用
Object.assign()
从这个对象数组构建一个对象:
const form = { first_name: "", last_name: "", avatar: "" }; const profile = { first_name: "John", last_name: "Doe", avatar: "https://image.cdn.com", foo: 1, bar: 2, foobar: 3 }; const lift = (o1, o2) => Object.assign({}, ...Object.keys(o1).map(k => ({[k]: o2[k]}))); const res = lift(form, profile); console.log(res);
Or, since you're using lodash in your example, you can use lodash's _.pick()
method with _.keys()
like so:或者,因为你使用lodash在你的榜样,您可以使用lodash的
_.pick()
与方法_.keys()
像这样:
const form = { first_name: "", last_name: "", avatar: "" }; const profile = { first_name: "John", last_name: "Doe", avatar: "https://image.cdn.com", foo: 1, bar: 2, foobar: 3 }; const res = _.pick(profile, _.keys(form)); console.log(res);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>
You can use Object.assign()
or ES6 spread operator to create a new object您可以使用
Object.assign()
或 ES6 扩展运算符来创建新对象
let profile= { first_name: "John", last_name: "Doe", avatar: "https://image.cdn.com", key1: 1, key2: 2, key: 3, } let form= { first_name: "", last_name: "", avatar: "" } let output = { ...form, ...profile }; console.log(output);
And if you want to don't want to override the values in source object if value is not falsy/empty then you can do something like并且如果您不想在 value 不是 falsy/empty 的情况下覆盖源对象中的值,那么您可以执行类似的操作
let profile= { first_name: "John", last_name: "Doe", avatar: "https://image.cdn.com", key1: 1, key2: 2, key: 3, } let form= { first_name: "Adam", last_name: "", avatar: "" } const output = Object.keys(form).reduce((prev, key) => { prev[key] = form[key] || profile[key]; return prev; }, {}); console.log(output);
One simple solution is creating your own merging function (but to check your real need.. eg if on form variable an attributes is not empty.. which value will win ? but you can adjust my example according eg一个简单的解决方案是创建您自己的合并函数(但要检查您的真正需要。
function getOnlyExisting(aParam1, aParam2) {
var vResult = Object.create(aParam1);
for (var vAttributeName in aParam1) {
if (aParam2.hasOwnProperty(vAttributeName)) {
vResult[vAttributeName] = aParam2[vAttributeName];
}
}
return vResult;
}
let profile = {
first_name: "John",
last_name: "Doe",
avatar: "https://image.cdn.com",
key1: 1,
key2: 2,
key: 3,
}
let form = {
first_name: "",
last_name: "",
avatar: ""
}
console.log(getOnlyExisting(form, profile));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.