admin管理员组文章数量:1332973
how can I make lodash's pickby function in javascript? I have found the following one in "you don't need lodash"
function pickBy(object) {
const obj = {};
for (const key in object) {
if (object[key] !== null && object[key] !== false && object[key] !== undefined) {
obj[key] = object[key];
}
}
return obj;
}
but wondering other implementations
how can I make lodash's pickby function in javascript? I have found the following one in "you don't need lodash"
function pickBy(object) {
const obj = {};
for (const key in object) {
if (object[key] !== null && object[key] !== false && object[key] !== undefined) {
obj[key] = object[key];
}
}
return obj;
}
but wondering other implementations
Share Improve this question edited Feb 18, 2019 at 10:12 Wasif Ali 8941 gold badge13 silver badges30 bronze badges asked Feb 18, 2019 at 9:24 watthecodewatthecode 1012 silver badges7 bronze badges 2- you miss the optional predicate. but why do you have all the checks? – Nina Scholz Commented Feb 18, 2019 at 9:27
- Object.keys(obj).reduce((prev, x) => callback(obj[x]) ? { ...prev, [x] : obj[x] } : prev , {}); – alejoko Commented Feb 18, 2019 at 10:33
5 Answers
Reset to default 5You could add a predicate function, as _.pickBy
describes and use the entries and filter the data and build a new object.
function pickBy(object, predicate = v => v) {
return Object.assign(
...Object
.entries(object)
.filter(([, v]) => predicate(v))
.map(([k, v]) => ({ [k]: v }))
);
}
To create _.pickBy()
, you can use for...of
with Object.entries()
. If the predicate returns a truthy answer for the value, assign the key and value to the result object.
Note: if you need _.pickBy()
, and you don't want to entire lodash package, you can import the pickBy module.
function pickBy(object, predicate = v => v) {
const obj = {};
for (const [key, value] of Object.entries(object)) {
if (predicate(value)) obj[key] = value;
}
return obj;
}
console.log(pickBy({ a: 1, b: 0, c: 3 }));
console.log(pickBy({ a: 1, b: 0, c: 3 }, v => v < 3 ));
Now days you can get the entries, filter by the value, and convert back to an object with Object.fromEntries()
:
const pickBy = (object, predicate = v => v) =>
Object.fromEntries(Object.entries(object).filter(([, v]) => predicate(v)))
console.log(pickBy({ a: 1, b: 0, c: 3 }));
console.log(pickBy({ a: 1, b: 0, c: 3 }, v => v < 3 ));
Being
var object = { 'a': 1, 'b': '2', 'c': 3 };
and callback a method which is applied just to the values of the object (_.isNumber):
Object.keys(obj).reduce((prev, x) => callback(obj[x]) ? { ...prev, [x] : obj[x] } : prev , {});
Nina's solution fails when the object is {}
.
Here is mine:
const pickBy = (object, predicate = v => v) =>
Object.entries(object)
.filter(([k, v]) => predicate(v))
.reduce((acc, [k, v]) => ({ ...acc, [k]: v }), {});
Here is a one-liner pickBy
:
const pickBy = (o, predicate = x => x) =>
Object.fromEntries(Object.entries(o).filter(([, v]) => predicate(v)))
and in TypeScript:
const pickBy = (
o: Record<string, any>,
predicate: (value: any) => Boolean = (x) => x,
) => Object.fromEntries(Object.entries(o).filter(([, v]) => predicate(v)))
Here is a demo
const pickBy = (o, predicate) => Object.fromEntries(Object.entries(o).filter(([_, v]) => predicate(v)));
// The source object
const obj = {
Name: "sample",
password: 123456,
username: "foo"
}
// Using the _.pickBy() method
console.log(pickBy(obj, Number.isInteger));
本文标签: converting lodash39s pickby in to javascriptStack Overflow
版权声明:本文标题:converting lodash's pickby in to javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742345581a2457453.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论