对象道具,在对象道具中组合

时间:2016-09-19 16:13:45

标签: javascript

例如,我们有如下对象:

let data = {
  firstName: '',
  lastName: '',
  "other.ref": '',
  "other.ref1": '',
  "other.ref2": '',
  "other.ref3": '',
  "other1.ref": '',
  "other1.ref1": '',
  "other1.ref2": '',
  "other1.ref3": ''
}

我希望得到下一个对象:

let data = {
  firstName: '',
  lastName: '',
  other: {
    ref: '',
    ref1: '',
    ref2: '',
    ref3: ''
  },
  other1: {
    ref: '',
    ref1: '',
    ref2: '',
    ref3: ''
  }
}

最高性能方式是什么? 谢谢。

3 个答案:

答案 0 :(得分:1)

您只需遍历属性名称并创建一个新对象,复制名称中没有.的属性,直接分割属性名称,并使用这些从属键创建从属对象:



let data = {
  firstName: '',
  lastName: '',
  "other.ref": '',
  "other.ref1": '',
  "other.ref2": '',
  "other.ref3": '',
  "other1.ref": '',
  "other1.ref1": '',
  "other1.ref2": '',
  "other1.ref3": ''
};
data = Object.keys(data).reduce(function(obj, key) {
  // See if this key has a dot in it
  var splitAt = key.indexOf(".");
  if (splitAt === -1) {
    // No, just copy the value
    obj[key] = data[key];
  } else {
    // Yes, get the first part and the rest of it
    var subKey = key.substring(0, splitAt);
    var subPropKey = key.substring(splitAt + 1);
    
    // Get that subordinate object from our target object, if there
    var subObj = obj[subKey];
    if (!subObj) {
      // not there, add it
      obj[subKey] = subObj = {};
    }
    // Set the subordinate object's properly
    subObj[subPropKey] = data[key];
  }
  return obj;
}, {});
console.log(data);




答案 1 :(得分:1)

您可以迭代密钥并拆分它们。如果找到数组中的多个元素,则在必要时生成新对象。然后分配值并删除旧属性。



var data = { firstName: '', lastName: '', "other.ref": '', "other.ref1": '', "other.ref2": '', "other.ref3": '', "other1.ref": '', "other1.ref1": '', "other1.ref2": '', "other1.ref3": '' };

Object.keys(data).forEach(function (k) {
    var path = k.split('.'),
        last = path.pop();

    if (path.length) {
        path.reduce((r, a) => r[a] = r[a] || {}, data)[last] = data[k];
        delete data[k];
    }
});

console.log(data);

.as-console-wrapper { max-height: 100% !important; top: 0; }




答案 2 :(得分:0)

让我们做一些时髦的JavaScripting ......

var proto = {setNestedValue: function(...a) {
                               a.length > 2 ? typeof this[a[0]] === "object" && this[a[0]] !== null ? this.setNestedValue.apply(this[a[0]],a.slice(1))
                                                                                                    : (this[a[0]] = typeof a[1] === "string" ? {} : Array(a[1]),
                                                                                                       this.setNestedValue.apply(this[a[0]],a.slice(1)))
                                            : this[a[0]] = a[1];
                               return this;
                             },
                  nestProps: function(){
                               for(var prop in this) {
                                 var props = prop.split(".").concat(this[prop]);
                                 props.length > 2 && (this.setNestedValue(...props), delete this[prop]);
                               }
                             }
            }, // end of prototype
     data = {
  firstName: '',
  lastName: '',
  "other.ref": '',
  "other.ref1": '',
  "other.ref2": '',
  "other.ref3": '',
  "other1.ref": '',
  "other1.ref1": '',
  "other1.ref2": '',
  "other1.ref3": ''
  },
  newData = Object.assign(Object.create(proto),data);
  newData.nestProps();
console.log(JSON.stringify(newData,null,4));