0

Problem I have a Javascript array and I'm trying to return only the key:value pairs that match a specific key in the most efficient way possible.

I've tried using a Foreach loop and pushing each key:value to a new array but that seems like an inefficient way to do it and overly complex.

Code

initialArray = [ { friends: [],
    otherFriends: [],
    _id: 5c6c7132f9bf4bdab9c906ff,
    user: 5c65d9438e4a834c8e85dd7d,
    password: '$2b$10$LMFB6CBdwxSAfjog/Uo3t.u/G0GBtzfJnYdpvlrSNchA9.jlNOdAa',
    email: '[email protected]',
    createdAt: 2019-02-19T21:12:18.569Z,
    updatedAt: null,
    online: false,
    __v: 0 },
  { friends: [],
    otherFriends: [],
    _id: 5c6ccd6d3a0dc4e4951c2bee,
    user: 5c65d9438e4a834c8e85dd7e,
    password: '$2b$10$gEAbOAdKyHJfAksr5txUNOudCautRs1w/pubplQKzZ5PefhfOOEhq',
    email: '[email protected]',
    createdAt: 2019-02-20T03:45:49.703Z,
    updatedAt: null,
    online: false,
    __v: 0 } ] 

Desired Array

[ 
    { user: 5c65d9438e4a834c8e85dd7d },
    { user: 5c65d9438e4a834c8e85dd7e }
] 

Current Method

const newArray = []
initialArray.forEach(element => {
            newArray.push({key: element.user});
        });

If anyone has any suggestions that would be amazing! Thanks in advance :)

4
  • Are you querying the mongoose model yourself or is it an api response? :) Commented Apr 11, 2019 at 20:33
  • I’m querying mongoose myself Commented Apr 11, 2019 at 20:34
  • Then you can use a projection and only query for the user property. MyModel.find({}, 'user'); Commented Apr 11, 2019 at 20:34
  • If you also want to strip _id do MyModel.find({}, {'user': 1, '_id': 0});' Commented Apr 11, 2019 at 20:42

2 Answers 2

3

You could use a destructuring assignment for user and map this short hand property.

var initialArray = [{ friends: [], otherFriends: [], _id: '5c6c7132f9bf4bdab9c906ff', user: '5c65d9438e4a834c8e85dd7d', password: '$2b$10$LMFB6CBdwxSAfjog/Uo3t.u/G0GBtzfJnYdpvlrSNchA9.jlNOdAa', email: '[email protected]', createdAt: '2019-02-19T21:12:18.569Z', updatedAt: null, online: false, __v: 0 }, { friends: [], otherFriends: [], _id: '5c6ccd6d3a0dc4e4951c2bee', user: '5c65d9438e4a834c8e85dd7e', password: '$2b$10$gEAbOAdKyHJfAksr5txUNOudCautRs1w/pubplQKzZ5PefhfOOEhq', email: '[email protected]', createdAt: '2019-02-20T03:45:49.703Z', updatedAt: null, online: false, __v: 0 }] 
    newArray = initialArray.map(({ user }) => ({ user }));

console.log(newArray);

Sign up to request clarification or add additional context in comments.

1 Comment

This one will be faster because destructuring and spread is faster than object.assign with a new object
1

You can keep the desired keys in an array as follow.

This example extracts two key-values.

var initialArray = [{ friends: [], otherFriends: [], _id: '5c6c7132f9bf4bdab9c906ff', user: '5c65d9438e4a834c8e85dd7d', password: '$2b$10$LMFB6CBdwxSAfjog/Uo3t.u/G0GBtzfJnYdpvlrSNchA9.jlNOdAa', email: '[email protected]', createdAt: '2019-02-19T21:12:18.569Z', updatedAt: null, online: false, __v: 0 }, { friends: [], otherFriends: [], _id: '5c6ccd6d3a0dc4e4951c2bee', user: '5c65d9438e4a834c8e85dd7e', password: '$2b$10$gEAbOAdKyHJfAksr5txUNOudCautRs1w/pubplQKzZ5PefhfOOEhq', email: '[email protected]', createdAt: '2019-02-20T03:45:49.703Z', updatedAt: null, online: false, __v: 0 }],
    keys = ["user", "email"], // for the desired output in the question remove "email"
    newArray = initialArray.map(o => keys.reduce((a, k) => Object.assign({}, a, {[k]: o[k]}), {}));

console.log(newArray);
.as-console-wrapper { min-height: 100%; }

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.