【问题标题】:Update object property in nested array using Javascript or Lodash使用 Javascript 或 Lodash 更新嵌套数组中的对象属性
【发布时间】:2020-06-18 05:16:04
【问题描述】:

我有嵌套数组,每个对象都包含唯一的 path 属性。所以我想根据条件更新value 属性。下面是参考 JSON 对象

所以要求是在path=httpgateway.concurrency.cacheConfig.localConcurrent.0.servers.1下面的JSON中更新对象

假设当前值为localhost:9011,但我想要它localhost:9012

[{
    "name": "httpgateway",
    "type": "Object",
    "value": [
        {
            "name": "concurrency",
            "type": "Object",
            "value": [
                {
                    "path": "httpgateway.concurrency",
                    "name": "stalePeriod",
                    "type": "PORT",
                    "value": "3000"
                },
                {
                    "name": "cacheConfig",
                    "type": "Object",
                    "value": [
                        {
                            "name": "localConcurrent",
                            "type": "Object",
                            "value": [
                                {
                                    "name": "",
                                    "type": "Array",
                                    "value": [
                                        {
                                            "path": "httpgateway.concurrency.cacheConfig.localConcurrent.0",
                                            "name": "service",
                                            "type": "TEXT",
                                            "value": "/mock/test"
                                        },
                                        {
                                            "name": "servers",
                                            "type": "Object",
                                            "value": [
                                                {
                                                    "name": "",
                                                    "type": "Array",
                                                    "value": [
                                                        {
                                                            "path": "httpgateway.concurrency.cacheConfig.localConcurrent.0.servers.0",
                                                            "name": "hostName",
                                                            "type": "URL",
                                                            "value": "localhost:9010"
                                                        },
                                                        {
                                                            "path": "httpgateway.cacheConfig.localConcurrent.0.servers.0",
                                                            "name": "concurrency",
                                                            "type": "NUMBER",
                                                            "value": "5"
                                                        }
                                                    ]
                                                },
                                                {
                                                    "name": "",
                                                    "type": "Array",
                                                    "value": [
                                                        {
                                                            "path": "httpgateway.concurrency.cacheConfig.localConcurrent.0.servers.1",
                                                            "name": "hostName",
                                                            "type": "URL",
                                                            "value": "localhost:9011"
                                                        },
                                                        {
                                                            "path": "httpgateway.cacheConfig.localConcurrent.0.servers.1",
                                                            "name": "concurrency",
                                                            "type": "NUMBER",
                                                            "value": "5"
                                                        }
                                                    ]
                                                }
                                            ]
                                        }
                                    ]
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ]
}]

【问题讨论】:

  • 你有没有尝试过?
  • 我可以使用 _.flow() 根据条件找到对象,有点不确定我们如何在嵌套中更新
  • @PuneetBhandari 我不明白你所说的“条件”是什么意思。您要更新的条件是什么?如果path 是某个字符串,您想更新对象键value
  • 没错@Kousha
  • @PuneetBhandari 发布了一个示例解决方案

标签: javascript json recursion lodash


【解决方案1】:

你可以像这样在纯 JS 中做到这一点:

const updateKey = (obj, path, value) => {
    if (obj.path === path) {
        obj.value = value;

        return obj;
    }

    if (!Array.isArray(obj.value)) {
        return obj;
    }

    obj.value = obj.value.map(item => updateKey(item, path, value));

    return obj;
};

const updated = updateKey(data, 'httpgateway.concurrency.cacheConfig.localConcurrent.0.servers.1', 'localhost:9012');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-18
    • 2018-07-31
    • 1970-01-01
    • 2022-01-14
    • 1970-01-01
    • 2018-10-18
    • 2022-09-23
    相关资源
    最近更新 更多