【发布时间】: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