简体   繁体   English

Lodash:过滤然后映射对象

[英]Lodash: filter then map over an object

Im trying to filter out objects with a delete: false to then only map and render this objects to screen but I'm not sure how to get it to work.我试图用delete: false过滤掉对象,然后只映射和渲染这个对象到屏幕,但我不知道如何让它工作。

Sample object示例对象

{
  "8xf0y6ziyjabvozdd253nd": {
    "id": "8xf0y6ziyjabvozdd253nd",
    "timestamp": 1467166872634,
    "title": "Udacity is the best place to learn React",
    "body": "Everyone says so after all.",
    "author": "thingtwo",
    "category": "react",
    "voteScore": 6,
    "deleted": false
  }
}

Method for finding key and map.查找键和映射的方法。

const {posts} = this.props
        return _.find(posts, { 'deleted': false })_.map(posts, post => {
            return (
                <div key={post.id}>

Basically, you can apply lodash#reduce to Object directly instead of getting all the keys first, and then iterating again.基本上,您可以将lodash#reduce直接应用于 Object 而不是先获取所有键,然后再次迭代。 And with reduce you can do the map and filtering together .使用reduce您可以同时进行映射和过滤

_.reduce(obj, (i, v, k)=> !v.deleted && !(i[k] = v) || i, {});

Let's create a working example for you, Here is a code snippet:让我们为您创建一个工作示例,这是一个代码片段:

 var obj = { "9ny0z4ziyjabvozdc713dr": { "id": "9ny0z4ziyjabvozdc713dr", "timestamp": 1467166879723, "title": "StackOverfow is the best place to learn Angular", "body": "bla bla bla bla.", "author": "myself", "category": "angular", "voteScore": 9, "deleted": true }, "8xf0y6ziyjabvozdd253nd": { "id": "8xf0y6ziyjabvozdd253nd", "timestamp": 1467166872634, "title": "Udacity is the best place to learn React", "body": "Everyone says so after all.", "author": "thingtwo", "category": "react", "voteScore": 6, "deleted": false } } var finalObj =_.reduce(obj, (i, v, k)=> !v.deleted && !(i[k] = v) || i, {}); console.log(finalObj);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>

EDIT编辑

For the specific case (of OP), just some key/value pair needs to be removed and construct a new object, then better to use lodash#omitBy to simply remove some entries from an object against some condition.对于(OP的)特定情况,只需要删除一些键/值对并构造一个新对象,然后最好使用lodash#omitBy来简单地根据某些条件从对象中删除一些条目。 Here is a simple example of this particular use.这是这种特殊用途的一个简单示例。

_.omitBy(obj, o=>o.deleted);

Here is the code snippet for this example:这是此示例的代码片段:

 var obj = { "9ny0z4ziyjabvozdc713dr": { "id": "9ny0z4ziyjabvozdc713dr", "timestamp": 1467166879723, "title": "StackOverfow is the best place to learn Angular", "body": "bla bla bla bla.", "author": "myself", "category": "angular", "voteScore": 9, "deleted": true }, "8xf0y6ziyjabvozdd253nd": { "id": "8xf0y6ziyjabvozdd253nd", "timestamp": 1467166872634, "title": "Udacity is the best place to learn React", "body": "Everyone says so after all.", "author": "thingtwo", "category": "react", "voteScore": 6, "deleted": false } } var finalObj =_.omitBy(obj, o=>o.deleted); console.log(finalObj);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>

However, If any transformation needed in between (Array or Object), or the output type needs to be customized then I will still prefer to suggest to use reduce.但是,如果在(数组或对象)之间需要任何转换,或者需要自定义输出类型,那么我仍然更愿意建议使用reduce。

If you want to skip lodash , the native API of javascript array will offer you a solution like the following:如果您想跳过lodash ,javascript array 的本机 API 将为您提供如下解决方案:

const { posts } = this.props;

return posts
    .find(post => !post.deleted)
    .map(post => {
        return (
            <div key={post.id}>...

If you want to filter, the easiest thing to use is an actual filter.如果要过滤,最容易使用的是实际过滤器。 I'm not very familiar with React, but the Lodash chain below will work, if React supports more complex chains.我对 React 不是很熟悉,但是如果 React 支持更复杂的链,下面的 Lodash 链将起作用。

I noticed you have a nested object, if that is intentional, then the code below will do the trick:我注意到你有一个嵌套的对象,如果这是故意的,那么下面的代码可以解决问题:

 // I added some fake posts to make the data more illustrative let posts = [{ "8xf0y6ziyjabvozdd253nd": { "id": "8xf0y6ziyjabvozdd253nd", "timestamp": 1467166872634, "title": "Udacity is the best place to learn React", "body": "Everyone says so after all.", "author": "thingtwo", "category": "react", "voteScore": 6, "deleted": false } }, { "8888ziyjabvozdd253nd": { "id": "8888ziyjabvozdd253nd", "timestamp": 1467166872634, "title": "Udacity is the best place to learn React", "body": "Everyone says so after all.", "author": "thingtwo", "category": "react", "voteScore": 6, "deleted": true } }, { "77776ziyjabvozdd253nd": { "id": "77776ziyjabvozdd253nd", "timestamp": 1467166872634, "title": "Udacity is the best place to learn React", "body": "Everyone says so after all.", "author": "thingtwo", "category": "react", "voteScore": 6, "deleted": false } }, { "6666ziyjabvozdd253nd": { "id": "6666ziyjabvozdd253nd", "timestamp": 1467166872634, "title": "Udacity is the best place to learn React", "body": "Everyone says so after all.", "author": "thingtwo", "category": "react", "voteScore": 6, "deleted": true } }, { "55556ziyjabvozdd253nd": { "id": "55556ziyjabvozdd253nd", "timestamp": 1467166872634, "title": "Udacity is the best place to learn React", "body": "Everyone says so after all.", "author": "thingtwo", "category": "react", "voteScore": 6, "deleted": false } }]; // you'd just return _(posts) here directly, but I'm assigning a variable so we can show the console log for this snippet const filteredPosts = _(posts) .filter(post => { const objectKey = Object.keys(post)[0]; const innerData = post[objectKey]; return innerData.deleted === false }) .map(post => { const objectKey = Object.keys(post)[0]; // your return would be in the format of <div key={objectKey}> return `<div key={${objectKey}}>` }) .valueOf(); console.log(filteredPosts);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>

If the nesting of the object isn't intentional, then getting the info will be more straightforward and cleaner.如果对象的嵌套不是故意的,那么获取信息将更加直接和清晰。 If your actual objects look like the sample ones I have below, you could access the data like this:如果您的实际对象看起来像我下面的示例对象,您可以像这样访问数据:

 let posts = [{ "id": "8xf0y6ziyjabvozdd253nd", "timestamp": 1467166872634, "title": "Udacity is the best place to learn React", "body": "Everyone says so after all.", "author": "thingtwo", "category": "react", "voteScore": 6, "deleted": false }, { "id": "8888ziyjabvozdd253nd", "timestamp": 1467166872634, "title": "Udacity is the best place to learn React", "body": "Everyone says so after all.", "author": "thingtwo", "category": "react", "voteScore": 6, "deleted": true }, { "id": "77776ziyjabvozdd253nd", "timestamp": 1467166872634, "title": "Udacity is the best place to learn React", "body": "Everyone says so after all.", "author": "thingtwo", "category": "react", "voteScore": 6, "deleted": false }]; // you'd just return _(posts) here directly, but I'm assigning a variable so we can show the console log for this snippet const filteredPosts = _(posts) .filter(post => { return post.deleted === false }) .map(post => { // your return would be in the format of <div key={post.id}> return `<div key={${post.id}}>` }) .valueOf(); console.log(filteredPosts);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>

The result of _.find is an array, so if you want to map the elements in that array you should _.map(result) _.find的结果是一个数组,所以如果你想map该数组中的元素,你应该_.map(result)

const {posts} = this.props;
return _.map(_.find(posts, { 'deleted': false }), post => {
            return (
                <div key={post.id}>

you can do that in the following way你可以通过以下方式做到这一点

 let obj = { "8xf0y6ziyjabvozdd253nd": { "id": "8xf0y6ziyjabvozdd253nd", "timestamp": 1467166872634, "title": "Udacity is the best place to learn React", "body": "Everyone says so after all.", "author": "thingtwo", "category": "react", "voteScore": 6, "deleted": false } } let result = Object.keys(obj).reduce((a, b) =>{ if(obj[b].deleted != false){ a[b] = obj[b]; } return a; }, {}); console.log(result);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM