0

I have nested array and each object contains unique path property. So i want to update value property based on condition. Below is reference JSON object

So requirement is to update object in below JSON where path=httpgateway.concurrency.cacheConfig.localConcurrent.0.servers.1

Say current value is localhost:9011 but i want it 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"
                                                        }
                                                    ]
                                                }
                                            ]
                                        }
                                    ]
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ]
}]
5
  • Have you tried anything? Commented Jun 18, 2020 at 5:17
  • I am able to find object based on condition using _.flow(), bit not sure how we can update in nested Commented Jun 18, 2020 at 5:19
  • @PuneetBhandari I don't understand what you mean by "condition". What is the condition you want to update? You want to update the object key value if the path is of a certain string? Commented Jun 18, 2020 at 5:21
  • Thats correct @Kousha Commented Jun 18, 2020 at 5:21
  • @PuneetBhandari posted a sample solution Commented Jun 18, 2020 at 5:32

1 Answer 1

1

You can do it in pure JS like so:

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');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.