3

Write a function called "select".

Given an array and an object, "select" returns a new object whose properties are those in the given object AND whose keys are present in the given array.

var arr = ['a', 'c', 'e'];
var obj = {
  a: 1,
  b: 2,
  c: 3,
  d: 4
};
var output = select(arr, obj);
console.log(output); // --> { a: 1, c: 3 }

My Solution:

function select(arr, obj) {
  for (var k in obj) {
    return arr.reduce((o, c) => {
      if (obj.hasOwnProperty(c)) {
        o[c] = obj[c]
      }
      return o
    }, {})
  }
}

var array = ['a', 'c', 'e'];

var object = {
  a: 1,
  b: 2,
  c: 3,
  d: 4
};

console.log(select(array, object));

My solution is working, but I have a feeling that I'm not using the best practices or most eloquent code. For example I'm using a for/in to search the object, but I never use the 'k' from for (var k in obj). Any tips would be appreciated.

4
  • 3
    Yes, just omit the for (var k in obj) {. It has absolutely no purpose in your code. Can you explain what you meant it to do? Commented Aug 29, 2017 at 12:25
  • I wanted to search over every property of the object @Bergi Commented Aug 29, 2017 at 12:26
  • 2
    @jhazelton1 you don't need to iterate over obj as you're using arr.reduce to do that. Commented Aug 29, 2017 at 12:26
  • And you are returning on the first iteration, so there won't be a lookup over all properties of the object either way Commented Aug 29, 2017 at 12:30

3 Answers 3

1

You can use Object.assign() with reduce() and inside check if property exists in object.

var arr = ['a', 'c', 'e'];
var obj = { a: 1,b: 2,c: 3, d: 4};

let select = (arr, obj) => arr.reduce((r, e) => Object.assign(r, obj[e] ? {[e]: obj[e]} : null), {})

var output = select(arr, obj);
console.log(output); // --> { a: 1, c: 3 }

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

1 Comment

Don't pass a string to Object.assign. Pass null.
1

To get the intersection between the keys of the objects and the keys in the array, there are two approaches:

  • enumerate the object property keys and check which of them are also in the array
  • iterate the keys in the array and check which of them are also an object property

You don't have to do both loops. The first approach will be more efficient for small objects, the second one works well for large objects and small subsets as well.

function select(arr, obj) {
  let o = {};
  for (let k in obj)
    if (arr.includes(k))
      o[k] = obj[k];
  return o;
}

function select(arr, obj) {
  let o = {};
  for (let k of arr)
    if (k in obj)
      o[k] = obj[k];
  return o;
}

You can also use reduce instead of the for … of loop like you successfully did (I won't repeat that solution), but which of the two is easier to read and understand only you can decide.

5 Comments

I think you are missing a curly brace.
@LaravelSucks Rather, having one too much :-) Thanks, fixed!
This only matches the first key and then it returns.
@London804 No it doesn't? The return statements are after the respective loop
@Bergi I'm sorry you're right. It looks like my console.log was in the loop. I will upvote as soon as it lets me.
0

Iterate the array with Array#reduce and pluck all properties that exist on the object to a new object:

const arr = ['a', 'c', 'e'];
const obj = {
  a: 1,
  b: 2,
  c: 3,
  d: 4
};

const select = (arr, obj) => arr.reduce((r, prop) => {
    obj.hasOwnProperty(prop) && (r[prop] = obj[prop]);
    
    return r;
  }, {});

const output = select(arr, obj);
console.log(output); // --> { a: 1, c: 3 }

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.