简体   繁体   English

如何将一个对象的值复制到另一个对象的键中?

[英]How to copy the values of an object into the keys of another object?

I have 2 Objects. 我有2个对象。

Object1: Object1:

      {
         "a": "XXXXX",
         "b": "YYYYY",
         "c": "ZZZZZ"
      }

and Object2: 和Object2:

    {
        "a": "KKKKK",
        "b": "LLLLL",
        "c": "MMMMM"
    } 

both have the same length and same keys at the same index, but different values. 两者在相同的索引处具有相同的长度和相同的键,但是值不同。

Result should be: 结果应为:

    {
        "XXXXX": "KKKKK",
        "YYYYY": "LLLLL",
        "ZZZZZ": "MMMMM"
    } 

i've tried: 我试过了:

    for (let el in Object2){
      el = Object1[el]      
    }

but its not working. 但它不起作用。 What i'm doing wrong? 我做错了什么?

const x = {
   "a": "XXXXX",
   "b": "YYYYY",
   "c": "ZZZZZ"
}

const y = {
  "a": "KKKKK",
  "b": "LLLLL",
  "c": "MMMMM"
} 

const z = Object.keys(x).reduce((acc, key) => {
  acc[x[key]] = y[key]
  return acc
},{})

console.log(z)

We can use Array.prototype.reduce and Object.keys to iterate over the keys of the first object and use its values as the keys for the result object: 我们可以使用Array.prototype.reduceObject.keys遍历第一个对象的键,并将其值用作result对象的键:

 const obj1 = { "a": "XXXXX", "b": "YYYYY", "c": "ZZZZZ" } const obj2 = { "a": "KKKKK", "b": "LLLLL", "c": "MMMMM" } const result = Object.keys(obj1).reduce((acc, ele, idx) => { acc[obj1[ele]] = obj2[ele]; return acc; }, {}); console.log(result); 

You can also use Object.assign with Object.values : 您还可以将Object.assignObject.values一起Object.values

 var obj1 = { "a": "XXXXX", "b": "YYYYY", "c": "ZZZZZ" }; var obj2 = { "a": "KKKKK", "b": "LLLLL", "c": "MMMMM" }; var obj3 = Object.assign(...Object.values(obj1).map((k, i) => ({[k]: Object.values(obj2)[i]}))); console.log(obj3); 

Or, use a forEach : 或者,使用forEach

 var obj1 = { "a": "XXXXX", "b": "YYYYY", "c": "ZZZZZ" }; var obj2 = { "a": "KKKKK", "b": "LLLLL", "c": "MMMMM" }; var obj3 = {}; Object.values(obj1).forEach((key, i) => obj3[key] = Object.values(obj2)[i]); console.log(obj3); 

you can use Object.Values + reduce something like this: 您可以使用Object.Values + reduce如下内容:

 const a = { "a": "XXXXX", "b": "YYYYY", "c": "ZZZZZ" } const b = { "a": "KKKKK", "b": "LLLLL", "c": "MMMMM" } var res = Object.values(a).reduce((newObject, value, index) => { newObject[value] = Object.values(b)[index] return newObject; }, {}); console.log(res) 

We can use Array.prototype.reduce and Object.entries to iterate over the keys of the first object and use its values as the keys for the result object: 我们可以使用Array.prototype.reduceObject.entries遍历第一个对象的键,并将其值用作result对象的键:

 const obj1 = { "a": "XXXXX", "b": "YYYYY", "c": "ZZZZZ" } const obj2 = { "a": "KKKKK", "b": "LLLLL", "c": "MMMMM" } const result = Object.entries(obj1).reduce((acc, ele) => { acc[ele[1]] = obj2[ele[0]]; return acc; }, {}); console.log(result); 

You can loop through the keys and set the key and value of the output object based on the value in the input objects 您可以循环浏览键,并根据输入对象中的值设置输出对象的键和值

 const o1 = { a: "XXXXX", b: "YYYYY", c: "ZZZZZ" }, o2 = { a: "KKKKK", b: "LLLLL", c: "MMMMM" }, output = {}; for (let key in o1) output[o1[key]] = o2[key] console.log(output) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM