2

Creates an object composed of the picked source properties.

Parameters

  • source - Any JavaScript Object
  • keys - An array of JavaScript Strings

Return Value

A new Object containing all of the properties of source listed in keys. If a key is listed in keys, but is not defined in source, then that property is not added to the new Object.

Examples

pick({ foo: 1, bar: 2, baz: 3 }, ['foo', 'baz']) // -> { foo: 1, baz: 3 }
pick({ qux: 4, corge: 5 }, ['bar', 'grault'])    // -> {}
pick({ bar: 2 }, ['foo', 'bar', 'baz'])          // -> { bar: 2 }

I have

function pick(source, keys) {
  let result = {};
  for (key in source) {
    if (key === keys) {
      result[key];
    }
  }
  return result;
}

so far

Barmar
  • 741,623
  • 53
  • 500
  • 612
tryingToBeBetter
  • 365
  • 1
  • 6
  • 16
  • 1
    What are you expecting the statement `result[key];` to do? When is a `key` (a string) going to be exactly equal to `keys` (an array)? – Klaycon Jul 01 '20 at 18:43
  • Does this answer your question? [How to get a subset of a javascript object's properties](https://stackoverflow.com/questions/17781472/how-to-get-a-subset-of-a-javascript-objects-properties) – Heretic Monkey Jul 01 '20 at 18:55

4 Answers4

5

You're not assigning anything to result[key], that should be result[key] = source[key].

You're not testing whether key is in keys correctly. === does exact comparison, you want to use keys.includes(key) to test inclusion.

function pick(source, keys) {
  let result = {};
  for (key in source) {
    if (keys.includes(key)) {
      result[key] = source[key];
    }
  }
  return result;
}

console.log(pick({ foo: 1, bar: 2, baz: 3 }, ['foo', 'baz'])) // -> { foo: 1, baz: 3 }
console.log(pick({ qux: 4, corge: 5 }, ['bar', 'grault']))    // -> {}
console.log(pick({ bar: 2 }, ['foo', 'bar', 'baz']))          // -> { bar: 2 }
Barmar
  • 741,623
  • 53
  • 500
  • 612
2

you can use Object.entries to iterate through the values and keys

function pick(object,arr){
  o={}
      Object.entries(object).forEach(x=>{
        if(arr.includes(x[0]))   o[x[0]]=x[1]
      })
      return o
}
console.log(pick({ foo: 1, bar: 2, baz: 3 }, ['foo', 'baz'])) // -> { foo: 1, baz: 3 }
console.log(pick({ qux: 4, corge: 5 }, ['bar', 'grault']) )   // -> {}
console.log(pick({ bar: 2 }, ['foo', 'bar', 'baz']))        // -> { bar: 2 }
Sven.hig
  • 4,449
  • 2
  • 8
  • 18
2

You can check if each key exists in your object then add it to the new object and finally return it.

function pick(src, keys) {
  var newObj = {};
  keys.forEach(key => key in src && (newObj[key] = src[key]));
  return newObj;
}

console.log(pick({ foo: 1, bar: 2, baz: 3 }, ['foo', 'baz']));
console.log(pick({ qux: 4, corge: 5 }, ['bar', 'grault']));
console.log(pick({ bar: 2 }, ['foo', 'bar', 'baz']));
Saadi Toumi Fouad
  • 2,779
  • 2
  • 5
  • 18
2

Instead of iterating through all the object keys (not all of which may be of interest), it would be more efficient to iterate through the array of desired keys. Also, since testing whether an object has a key and getting the key's value would require repeating some of the same internal operations, it may be faster to just attempt to get the value and test for undefined.

function pick(obj, keys) {
  var res = {};
  for (k of keys) {
    var v = obj[k];
    if (v != undefined)
      res[k] = v;
  }
  return res;
}

console.log(pick({ foo: 1, bar: 2, baz: 3 }, ['foo', 'baz'])) // -> { foo: 1, baz: 3 }
console.log(pick({ qux: 4, corge: 5 }, ['bar', 'grault']))    // -> {}
console.log(pick({ bar: 2 }, ['foo', 'bar', 'baz']))          // -> { bar: 2 }
cybersam
  • 63,203
  • 6
  • 53
  • 76