简体   繁体   English

Javascript Lodash - 替换对象中的对象

[英]Javascript Lodash - replace object in object

I have an object like this: 我有一个像这样的对象:

{
    "memory": {
        "memory1":{
            "schedule":"every day",
            "measure": [
                { "date": "date 1", "value": "10" }
            ]
        },
        "memory2": {
            "schedule":"every month",
            "measure": [
                { "date": "date 2", "value": "40" },
                { "date": "date 3", "value": "22" }
            ]
        },
        "memory3": {
            "schedule": "every day",
            "measure": []
        }
    },
    "loads": {
        "load1": {
            "schedule":"every day",
            "measure": [
                { "date": "date 4", "value": "40" }
            ]
        }
    }
}

Given a name (that in this object is memory and loads ) and a type (that in this object is memory1 , memory2 , memory2 for memory and load1 for loads 给定一个名称 (在此对象中是memoryloads )和一个类型 (在此对象中是memory1memory2memory2用于内存load1用于加载

I need to add another object to measure element, searching for his name and type. 我需要添加另一个对象来测量元素,搜索他的名字和类型。

For example, given memory and memory2 as variable value and an object { "date": "date 1", "value": "10" } , i will need to concatenate this to measure on the second element (that is memory2) in the first tree that is memory. 例如,给定内存memory2作为变量值和一个对象{ "date": "date 1", "value": "10" } ,我将需要连接它来测量第二个元素(即memory2)第一棵树就是记忆。

I will need too maintain the "measure" array no more than 10 elements (FIFO strategy) but this is another story.... 我还需要维护“measure”数组不超过10个元素(FIFO策略),但这是另一个故事....

Right now i have created an new object with the new measure, but how to "replace" or "concat" it in the right tree place? 现在我用新措施创建了一个新对象,但如何在正确的树位“替换”或“连接”它?

The result should be: 结果应该是:

{
    "memory": {
        "memory1":{
            "schedule":"every day",
            "measure": [
                { "date": "date 1", "value": "10" }
            ]
        },
        "memory2": {
            "schedule":"every month",
            "measure": [
                { "date": "date 2", "value": "40" },
                { "date": "date 3", "value": "22" },
                { "date": "date 3", "value": "22" }  THIS IS THE ELEMENT TO ADD
            ]
        },
        "memory3": {
            "schedule": "every day",
            "measure": []
        }
    },
    "loads": {
        "load1": {
            "schedule":"every day",
            "measure": [
                { "date": "date 4", "value": "40" }
            ]
        }
    }
}

In order to access and modify the nested object property values dynamically you can utilize an Object method like Object.prototype.getNestedValue() It takes a series of strings or integers as arguments (strings for object properties and integers for array indices) in the order of how they are nested. 为了动态访问和修改嵌套对象属性值,您可以使用Object.prototype.getNestedValue()类的Object方法。它按Object.prototype.getNestedValue()将一系列字符串或整数作为参数(对象属性的字符串和数组索引的整数)它们是如何嵌套的。 Lets see... 让我们来看看...

 Object.prototype.getNestedValue = function(...a) { return a.length > 1 ? (this[a[0]] !== void 0 && this[a[0]].getNestedValue(...a.slice(1))) : this[a[0]]; }; var data = { "memory": { "memory1":{ "schedule":"every day", "measure": [ { "date": "date 1", "value": "10" } ] }, "memory2": { "schedule":"every month", "measure": [ { "date": "date 2", "value": "40" }, { "date": "date 3", "value": "22" } ] }, "memory3": { "schedule": "every day", "measure": [] } }, "loads": { "load1": { "schedule":"every day", "measure": [ { "date": "date 4", "value": "40" } ] } } }, newMeasureData = { "date": "date 3", "value": "22" }; data.getNestedValue("memory","memory2","measure").push(newMeasureData); console.log(JSON.stringify(data,null,2)); 

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

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