admin管理员组

文章数量:1410730

Creates an object posed 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

Creates an object posed 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

Share Improve this question edited Jul 1, 2020 at 18:49 Barmar 784k57 gold badges548 silver badges659 bronze badges asked Jul 1, 2020 at 18:41 tryingToBeBettertryingToBeBetter 4251 gold badge7 silver badges25 bronze badges 2
  • 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 Commented Jul 1, 2020 at 18:43
  • Does this answer your question? How to get a subset of a javascript object's properties – Heretic Monkey Commented Jul 1, 2020 at 18:55
Add a ment  | 

4 Answers 4

Reset to default 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 parison, 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 }

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 }

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']));

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 }

本文标签: javascriptHow to creates an object with only the listed keysStack Overflow