在保留数据

时间:2017-12-27 10:37:38

标签: javascript

我有一个对象Object1,它是AngularForms的Datamodel,其中所有Form功能都是使用此模型构建的,并且在此阶段更改模型是不可行的。

Api以Object2格式请求数据,将数据发送到API我必须将Object1中的所有数据复制到Object2

这两个对象如下:只是关于不同两个对象的样本

object1 = {
    applicant: {
        name : "Abc",
        title : "Mr.",
        addr1 : "",
        addr2 : "",
        email : "someone@example.com"
    },
    nominee: {
        name : "Def",
        title : "Ms.",
        addr1 : "",
        email : "sometwo@example.com",
        mobile : ""
    }, 
    ...
}

object2 = {
    "key1_1": "Abc",
    "key1_2": "Mr.",
    "key1_3": "",
    "key1_4": "",
    "key1_5": "someone@example.com",
    "key2_1": "Def",
    "key2_2": "Ms.",
    "key2_3": "",
    "key2_4": "sometwo@example.com",
    "key2_5": "",
    ...
}

我希望能够从第一个对象转换为第二个对象。我已经编写了function,如下所示。因此,调用函数并将Object1作为参数传递给Object2,反之亦然。

function convertToObj2(object1){
    obj2 = {
        key1_1 : object1.applicant.name,
        key1_2 : object1.applicant.title,
        key1_3 : object1.applicant.addr1,
        key1_4 : object1.applicant.addr2,
        key1_5 : object1.applicant.email,
        key2_1 : object1.nominee.name,
        key2_2 : object1.nominee.title,
        key2_3 : object1.nominee.addr1,
        key2_4 : object1.nominee.email,
        key2_5 : object1.nominee.mobile,
        ...
    }

    return obj2;
}

function convertToObj1(object2){
    obj1 = {
        applicant: {
            name : object2.key1_1,
            title : object2.key1_2,
            addr1 : object2.key1_3,
            addr2 : object2.key1_4,
            email : object2.key1_5
        },
        nominee: {
            name : object2.key2_1,
            title : object2.key2_2,
            addr1 : object2.key2_3,
            email : object2.key2_4,
            mobile : object2.key2_5
        }, 
        ...
    }
    return obj1;
}

有没有更好的方法将这些对象相互映射,而无需手动将每个值分配给键,因为它很繁琐且每个对象包含300多个键?

  

注意:键的名称不是exapmle中显示的,它们是没有底层的普通单词,而不是alpha顺序。

5 个答案:

答案 0 :(得分:1)

function convertToObj2(object1){
  var tmpObj = {};
  var keys = Object.keys(object1);
  for(keyName of keys){
      tmpObj = Object.assign(tmpObj,object1[keyName]);
   }
return tmpObj;
}

调用此函数将object1转换为object2。

答案 1 :(得分:0)

也许这解决了你的问题(使用jquery):

function convertToObj2(object1) {
  var object2 = {};
  $.each(object1, function(key, obj) {
    $.each(obj, function(key1, val1) {
      object2[key1] = val1;
    });
  });
  return object2;
}

function convertToObj1(object2) {
  var object1 = {};
  $.each(object2, function(key, val) {
    var obj1Key = key.substring(0, key.indexOf('_'));
    var obj = {};
    obj[key] = val;
    if (object1[obj1Key] != null) {
      object1[obj1Key][key] = val;
    } else {
      object1[obj1Key] = obj;
    }
  });
  return object1;
}


var object2 = {
  key1_1: "value 1_1",
  key1_2: "value 1_2",
  key1_3: "value 1_3",
  key1_4: "value 1_4",
  key1_5: "value 1_5",
  key2_1: "value 2_1",
  key2_2: "value 2_2",
  key2_3: "value 2_3",
  key2_4: "value 2_4",
  key2_5: "value 2_5",
};


var object1 = {
  key1: {
    key1_1: "value 1_1",
    key1_2: "value 1_2",
    key1_3: "value 1_3",
    key1_4: "value 1_4",
    key1_5: "value 1_5",
  },
  key2: {
    key2_1: "value 2_1",
    key2_2: "value 2_2",
    key2_3: "value 2_3",
    key2_4: "value 2_4",
    key2_5: "value 2_5",
  }
};

$(function() {
  console.log(convertToObj2(object1));
  console.log(convertToObj1(object2));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

答案 2 :(得分:0)

我能想到的唯一方法是将关键值属性从object1映射到object2,而无需逐行手动写出它们是使用for in循环或for Object.keys(obj)循环。

您还可以查看lodash,jQuery或其他前端实用程序库。它们通常具有辅助函数来处理这些任务。

转换时,您希望为每个键添加前缀/后缀,以便您可以在两种对象类型之间来回转换,并识别您正在处理的对象类型。

我真的不建议使用for in loop,但希望下面的示例会在概念层面为您提供一些想法。

function convertToObj2(obj1) {
    var obj2Prefix = "__obj2__",
        result = {};

    var key;
    for (key in obj1) {
        if (obj1.hasOwnProperty(key)) {
            // Add the prefix to new obj2 key
            // for identification purposes 
            result[obj2Prefix + key] = obj1[key];
        }
    }  
    return result;
}

希望这会有所帮助:)

答案 3 :(得分:0)

感谢所有花费宝贵时间寻找解决方案的人。

我已经参考this回答解决了这个问题。

步骤1:为两个对象中的每个键创建关系

sortBy ( "fName" );

步骤2:创建一个函数,通过键名“quotes”

添加/读取属性
relationArr = 
    [
        ["applicant.name", "key1_1" ],
        ["applicant.title", "key1_2" ],
        ["applicant.addr1", "key1_3" ],
        ["applicant.addr2", "key1_4" ],
        ["applicant.email", "key1_5" ],
        ["nominee.name", "key2_1" ],
        ["nominee.title", "key2_2" ],
        ["nominee.addr1 ", "key2_3" ],
        ["nominee.email", "key2_4" ],
        ["nominee.mobile", "key2_5" ],
        ...
    ]

步骤3:创建一个接受源对象并返回转换后的对象w.r.t relationArr的函数,此处fromIndex和toIndex指定转换方向

function add_property(returnObj, key, sourceObj, srcKey) {
    if (key == null || key == "null"){
        return;
    }
    var keys = key.split('.');
    while (keys.length > 1) {
        var k = keys.shift();
        if (!returnObj.hasOwnProperty(k)) {
            returnObj[k] = {};
        }
        returnObj = returnObj[k];
    }
    returnObj[keys[0]] = this.read_property(sourceObj, srcKey);
}

function read_property(sourceObj, key) {
    if (key == null || key == "null"){
        return null;
    }
    var keys = key.split('.');
    while (keys.length > 1) {
        var k = keys.shift();
        sourceObj = sourceObj[k];
    }
    return sourceObj[keys[0]] != undefined ? sourceObj[keys[0]] : "";
}

所以,这样,如果我指定键的关系一次,我要转换对象类型,我只添加了两个关联的对象类型,如果有更多的对象只添加另一个对象的键{{1} }关系,将能够转换

function convertObject(relObj, sourceObj, fromIndex, toIndex){
    var returnObj = {};
    for (var i = 0, len = relObj.length; i < len; i++) {
        this.add_property(returnObj, relObj[i][toIndex], sourceObj, relObj[i][fromIndex] )
    }
    return returnObj;
}

答案 4 :(得分:-1)

您可以使用array#reduce

var object1 = { key1: { key1_1: "value 1_1", key1_2: "value 1_2", key1_3: "value 1_3", key1_4: "value 1_4", key1_5: "value 1_5", }, key2: { key2_1: "value 2_1", key2_2: "value 2_2", key2_3: "value 2_3", key2_4: "value 2_4", key2_5: "value 2_5", } },
    object2 = { key1_1: "value 1_1", key1_2: "value 1_2", key1_3: "value 1_3", key1_4: "value 1_4", key1_5: "value 1_5", key2_1: "value 2_1", key2_2: "value 2_2", key2_3: "value 2_3", key2_4: "value 2_4", key2_5: "value 2_5"};

var object1ToObject2 = (o) => {
  return Object.keys(o).reduce((r, k) => Object.assign(r,o[k]),{});
}

var object2ToObject1 = (o) => {
  return Object.keys(o).reduce((r,k) => {
    var key = k.split('_')[0];
    r[key] = r[key] || {};
    r[key][k] = o[k];
    return r;
  },{});
}

console.log(object1ToObject2(object1));
console.log(object2ToObject1(object2));