0

I need extract sub-object from state object using fields array, witch contains keys that I want extract.

And after, I need an Array with unused keys.

My input object

state = {id: '123', number: '456', extra_field: 'value'}
fields = ["id", "number", "identifier"]

Wanted results

missing_fields = ['identifier']
data = {id: '123', number: '456'}

I'm using lodash, if is util!

1 Answer 1

1

To select an object with specific keys, use _.pick:

const data = _.pick(state, fields);

To find missing keys, use _.difference of state keys from fields:

const missing = _.difference(fields, _.keys(state));
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.