-1

I have a state that looks like this:

attributes: [
{name: "capacity", items: [{ value: '256GB'},{value: '512GB'}]},
{name: 'With USB 3 ports', items: [{value: 'Yes'}, { value: 'No'}]}
]

I want to create a function which takes 2 paramaters, for example "capacity" and "256GB", I want to iterate over attributes state and find the object where the value property is 256GB and add another property in that object.

so I want the state to look like this:

attributes: [
{name: "capacity", items: [{ value: '256GB', selected: true},{value: '512GB', selected: false}]},
{name: 'With USB 3 ports', items: [{value: 'Yes'}, { value: 'No'}]}
]
4

1 Answer 1

0

There are multiple ways to do this, the easiest way to do this is to use in-build javascript functions like map, and reduce as shown in the example below

Added an example at codesandbox on how this could be achieved. The part you need to be careful is when working with objects/arrays that hold state in react, is that you do not intend to mutate them. It's out of scope for this question to explain but to get a clear understanding of it research how javascript passes parameters.

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.