简体   繁体   English

如何在不添加新属性的情况下从另一个深度复制 JavaScript object

[英]how to deep copy a JavaScript object from another without adding new properties

Here I have a default object with nested properties for eg -在这里,我有一个默认的 object,其中包含嵌套属性,例如 -

default = { 
key1: val1;
key2: {
  key3: val3,
  key4: val4
  }
}

and a new object with new values for same properties as above and some extra properties of its own和一个新的 object 具有与上面相同的属性的新值以及它自己的一些额外属性

newObject = {
key1: val11,
key2: {
  key3: val31,
  key5: val51,
  }
}

Now I need to replace all the available nested properties of default with newObject without adding any new property to default.现在我需要用 newObject 替换 default 的所有可用嵌套属性,而不向 default 添加任何新属性。 The skeleton of default should never change.默认的骨架永远不应该改变。 Also this needs to be a deep copy.这也需要是一个深拷贝。 So the result here should look like:所以这里的结果应该是这样的:

result = { 
key1: val11;
key2: {
  key3: val31,
  key4: val4,
  }
}

I added code for you but if you need more complex then you may want to use some js packages, but I think it should be sufficient.我为你添加了代码,但如果你需要更复杂的代码,那么你可能想要使用一些 js 包,但我认为这应该足够了。

Here you go:给你go:

let objectDefault = {
      key1: 'val1',
      key2: {
        key3: 'val3',
        key4: 'val4'
      }
    }
    
let otherObject = {
  key1: 'val5',
  key2: {
    key3: 'val6',
    key4: 'val7'
  },
  key10: 'test'
}

let filteredValuesOfOthedObject = Object.fromEntries(Object.entries(otherObject).filter(([key]) => Object.keys(objectDefault).includes(key)));

let newObject = {...objectDefault, ...filteredValuesOfOthedObject}

console.log(newObject)

Take a look at this one.看看这个。 // Parent Object let _default = { key1: 'val1', key2: { key3: 'val3', key4: 'val4', key6: { key7: 'test', key10: 'val10' } }, key11: 'val231' }; // Parent Object let _default = { key1: 'val1', key2: { key3: 'val3', key4: 'val4', key6: { key7: 'test', key10: 'val10' } }, key11: 'val231 '};

// Output object
let newObj = {
    key1: 'val11',
    key2: {
        key3: 'val31',
        key5: 'val51',
        key6: {
            key7: 'val31',
            key8: 'val43'
        },
        key12: 'val212'
    }
};

// Utils to check if value if object
function isObject(value) {
    return typeof (value) === 'object' && !(value instanceof Date) && !Array.isArray(value) && !Object.is(value, null) && !Object.is(value, undefined) && !(value instanceof Function)
}

// Recursive function to add 
function recursive(obj, ouputObj) {
    Object.keys(obj).map((el, i) => {
        let val = obj[el];
        if (!ouputObj?.[el]) ouputObj[el] = val;
        if (isObject(val)) recursive(val, ouputObj?.[el] || {});
    })
    return ouputObj;
}

newObj = recursive(_default, newObj);
console.log(`~~~~~~~~~~~~~~~, `, newObj);

暂无
暂无

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

相关问题 Javascript-将特定属性从一个对象深度复制到另一个新对象 - Javascript - Deep copy specific properties from one object to another new object 如何在不使用Javascript或jQuery添加属性的情况下将对象的属性复制到另一个对象? - How do you copy properties of an object into another without adding properties using Javascript or jQuery? 更新 object 数组而不添加来自另一个 object 的新属性 - Update object array without adding new properties from another object 将Javascript对象复制到另一个对象,而不是创建新属性 - Copy Javascript Object into another Object, not creating new Properties 如何在不覆盖Javascript的情况下将一个对象的属性添加到另一个对象中 - How to add properties from one object into another without overwriting in Javascript 如何有效地将属性从对象复制到另一个对象? - How to efficiently copy properties from an object to another? 将值从一个对象复制到另一个对象,忽略新属性 - Copy value from an object to another, ignoring new properties 如何在JavaScript中将对象深复制为对象而不是数组 - How to deep copy an object in javascript as object not as array 在javascript中没有对象引用的情况下将对象数组复制到另一个数组(深层复制) - Copying of an array of objects to another Array without object reference in javascript(Deep copy) 如何将 object 深层属性转换为另一个? - How to convert object deep properties in to another?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM