admin管理员组文章数量:1410730
Creates an object posed of the picked source properties.
Parameters
source
- Any JavaScript Objectkeys
- 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 Objectkeys
- 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 akey
(a string) going to be exactly equal tokeys
(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
4 Answers
Reset to default 5You'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
版权声明:本文标题:javascript - How to creates an object with only the listed keys? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745030730a2638479.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论