简体   繁体   English

如何使用最新的 Javascript 合并两个对象,使一个对象的键映射到另一个对象的值,而第二个对象没有额外的键?

[英]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.

相关问题 当一个 object 使用 Lodash 有额外的键时,你如何比较 JSON 对象中的键和值? - How do you compare keys & values in a JSON objects when one object has extra keys using Lodash? 当我将 2 个对象与.map() 合并时,如何为键赋值? - How do I assign values to keys when I merge 2 objects with .map()? 有没有办法使用扩展运算符合并两个对象,使得结果具有一个 object 的键,但第二个的值? - Is there way to use the spread operator to merge two objects such that the result has the keys of one object, but the values of the second? 如何合并两个不在主 object 中的没有键的对象? - How to merge two objects without keys which are not in the master object? 如何使用唯一键将两个或更多对象数组合并为一个 - how to merge two and more array of objects in one with unique keys 如何根据目标 object 的键合并 javascript 中的两个嵌套对象? - How to merge two nested objects in javascript based on the keys of the destination object? 如果某些键和值在 JavaScript 中匹配,如何合并对象? - How to merge objects if certain keys and values match in JavaScript? Javascript:如果对象具有匹配的键,如何用第二个对象的值替换第一个对象的值 - Javascript: How to replace first object's values with values from second object, if objects have matching keys 从基于两个键的对象的两个数组中查找唯一值,即在JavaScript中从and到 - finding unique values from two arrays of objects based on two keys i.e from and to in javascript Javascript 合并键和值 - Javascript Merge keys and values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM