【问题标题】:Javascript compare two object keys making new object [closed]Javascript比较创建新对象的两个对象键[关闭]
【发布时间】:2019-12-27 11:38:03
【问题描述】:

我有两个对象。如果一个键出现在两个对象中,我希望第一个对象的值作为键,第二个对象的值作为值。

var obj1 = {a:"value1",b:"value2",c:"value3"}
var obj2 = {a:"index1",b:"index2",c:"index3",d:"index4"};

输出应如下所示:

{"value1":"index1","value2":"index2","value3":"index3"}

我该怎么做?

【问题讨论】:

    标签: javascript arrays object ecmascript-6 lodash


    【解决方案1】:

    从第一个对象中获取条目 ([key, value]),如果 obj2 中也存在该键,则使用 reduce 与第二个对象中的值组合:

    const obj1 = {a:"value1",b:"value2",c:"value3"};
    const obj2 = {a:"index1",b:"index2",c:"index3",d:"index4"};
    
    const result = Object.entries(obj1) // get the entries
      .reduce((r, [k, v]) => {
        if(k in obj2) { // if the key exists in obj2
          r[v] = obj2[k]; // use the value from obj1 as the key, with the value from obj2
        }
      
        return r
      }, {});
      
    console.log(result);

    使用 lodash,您可以从两个对象中获取密钥,并使用 _.intersection() 查找公共密钥。然后你可以用_.at()从每个对象中获取值,然后用_.zipObject()将两个值数组组合成一个对象:

    const obj1 = {a:"value1",b:"value2",c:"value3"};
    const obj2 = {a:"index1",b:"index2",c:"index3",d:"index4"};
    
    // get the keys that exists in both objects
    const keys = _.intersection(_.keys(obj1), _.keys(obj2));
    const result = _.zipObject( // combine arrays of values to an object
      _.at(obj1, keys), // get the values from obj1
      _.at(obj2, keys), // get the values from obj2
    )
    
    console.log(result);
    <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js"></script>

    【讨论】:

      【解决方案2】:

      遍历obj1的键,使用obj1的值作为键,obj2的值作为值。

      const obj1 = {a:"value1",b:"value2",c:"value3"}
      const obj2 = {a:"index1",b:"index2",c:"index3",d:"index4"};
      
      const newObj = {};
      
      for (let key in obj1)
          if (obj2[key])
          	newObj[obj1[key]] = obj2[key];
      
      console.log(newObj);

      【讨论】:

      • 但是你没有比较键是否匹配,你假设它们是相同的,如果它们不匹配怎么办
      • @OnerT。编辑内容以检查密钥匹配
      【解决方案3】:

      var obj1 = {a:"value1",b:"value2",c:"value3"}
      var obj2 = {a:"index1",b:"index2",c:"index3",d:"index4"};
      
      var obj = {};
      
      for( let i in obj1) {
        if(obj2.hasOwnProperty(i))
          obj[ obj1[i] ] = obj2[i];
      }
      
      console.log(obj)

      【讨论】:

      • 您可以使用if(obj2.hasOwnProperty(i))if(i in obj2) 而不是创建一个键数组,并为每个i 检查includes
      • 当然!这很简单。将编辑它,谢谢!
      【解决方案4】:

      var obj1 = {a:"value1",b:"value2",c:"value3"}
      var obj2 = {a:"index1",b:"index2",c:"index3",d:"index4"};
      
      /* iterate on all the props of obj1*/
      const result=Object.keys(obj1).reduce((acc,obj1Prop)=>{
      /*if obj2[value of propObj1] is not undefined */
      if(typeof obj2[obj1Prop] !== "undefined"){
      // assign the value of obj2[value of propObj1] to accumulator
      	acc[obj1[obj1Prop]]=obj2[obj1Prop];
        }
        return acc;
      
      },{})
      //result contains the result
      console.log(result)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-07-19
        • 1970-01-01
        • 1970-01-01
        • 2019-08-02
        • 1970-01-01
        • 1970-01-01
        • 2020-12-26
        • 1970-01-01
        相关资源
        最近更新 更多