converting lodash's pickby in to javascript - Stack Overflow

how can I make lodash's pickby function in javascript? I have found the following one in "you

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
Add a ment  | 

5 Answers 5

Reset to default 5

You 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));

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742345581a4426475.html

相关推荐

  • converting lodash&#39;s pickby in to javascript - Stack Overflow

    how can I make lodash's pickby function in javascript? I have found the following one in "you

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信