Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

85
Views
find and update property of nested object with lodash

I have been scanning through stackoverflow topics and couldn't find answer to the problem i am having.

What i need to do is to find object inside nested (2 depths) array by some of the values and then update other of its values. I managed to put together the finding and also setting seems to work, the problem is that lodash does return main object and it updates main object, not the nested one i actually need.

lets take this structure:

var data = [
    {
        name: 'I',
        status: "0",
        categories: [
            {
                name: 'I1',
                status: "0",
                specifics: [
                    {
                        name: "I1a",
                        status: "0",
                    }
                ]
            }
        ]
    }
];

i need to find object inside specifics by its name and then update its status.

so lets try simple find first:

var find = _.find(data, { categories: [ { specifics: [ { name: "I1a" } ] } ]});

this is working but it returns the object with name: I so first main one.

so then if i try to update with this:

var set = _.set(_.find(data, { categories: [ { specifics: [ { name: "I1a" } ] } ]}), 'status', "1");

It also does work but it updates status of I instead of what i was looking for which is I1a.

Is there a way to make finding and therefore setting to return / work on actually queried object ?

    var data = [
        {
            name: 'I',
            status: "0",
            categories: [
                {
                    name: 'I1',
                    status: "0",
                    specifics: [
                        {
                            name: "I1a",
                            status: "0",
                        }
                    ]
                }
            ]
        }
    ];

var find = _.find(data, { categories: [ { specifics: [ { name: "I1a" } ] } ]})

console.log('find', find);

var set = _.set(_.find(data, { categories: [ { specifics: [ { name: "I1a" } ] } ]}), 'status', "1");

console.log('set', set);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!