1

I have two arrays and first array are having keys and want to check that all keys are having second array with value true. and return that true value array.

i have code as like below

const keysArray = ['phoneNo', 'name']

const data = [{ demo1:'abc', match_status:{ phoneNo: true, name: null }}, { demo2:'abc', match_status_flag:{ phoneNo: true, name: true }}]

My expected output as like below:

const outputArray = [{ demo2:'abc', match_status_flag:{ phoneNo: true, name: true }}]

i tried code as like below but not getting expected output

keysArray.map((k) => data.filter(i => i.match_status[k] === true))

1 Answer 1

3

You need a common property, like match_status_flag and filter the data.

const
    keys = ['phoneNo', 'name'],
    data = [{ demo1: 'abc', match_status_flag: { phoneNo: true, name: null } }, { demo2: 'abc', match_status_flag: { phoneNo: true, name: true } }],
    result = data.filter(({ match_status_flag }) =>
        keys.every(k => match_status_flag[k])
    );

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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.