Vignesh Sharma
Vignesh Sharma

Reputation: 31

Selectively return part of the matched object in filter

How to selectively return only a part of the matched object while iterating through the filter method of the list.

For Eg:

let comments = [{
    "postId": 6,
    "status": "ACTIVE",
    "id": 28,
    "name": "quo voluptates voluptas nisi veritatis dignissimos dolores ut officiis",
    "email": "[email protected]",
    "body": "voluptatem repellendus quo alias at laudantium\nmollitia quidem esse\ntemporibus consequuntur vitae rerum illum\nid corporis sit id"
  },
  {
    "postId": 6,
    "id": 29,
    "status": "INACTIVE",
    "name": "eum distinctio amet dolor",
    "email": "[email protected]",
    "body": "tempora voluptatem est\nmagnam distinctio autem est dolorem\net ipsa molestiae odit rerum itaque corporis nihil nam\neaque rerum error"
  }
];

comments.filter((ob, i) => {
  return ob.status == "ACTIVE" && ob.id
})

here, running filter on comments returns the matched object, but I want to return only list of id

Upvotes: 1

Views: 58

Answers (6)

StepUp
StepUp

Reputation: 38154

You can use reduce method and check the condition c.status == "ACTIVE" && c.id. If condition is satisfied, then you can push value into array:

const result = comments.reduce((a, c)=> {
   if (c.status == "ACTIVE" && c.id)
       a.push(c.id);
   return a;
}, [])

An example:

let comments = [{
  "postId": 6,
  "status": "ACTIVE",
  "id": 28,
  "name": "quo voluptates voluptas nisi veritatis dignissimos dolores ut officiis",
  "email": "[email protected]",
  "body": "voluptatem repellendus quo alias at laudantium\nmollitia quidem esse\ntemporibus consequuntur vitae rerum illum\nid corporis sit id"
},
{
  "postId": 6,
  "id": 29,
  "status": "INACTIVE",
  "name": "eum distinctio amet dolor",
  "email": "[email protected]",
  "body": "tempora voluptatem est\nmagnam distinctio autem est dolorem\net ipsa molestiae odit rerum itaque corporis nihil nam\neaque rerum error"
}
];

const result = comments.reduce((a, c)=> {
  if (c.status == "ACTIVE" && c.id)
    a.push(c.id);
  return a;
  }, [])

console.log(result);

Upvotes: 1

Zeeshan Afzal Satti
Zeeshan Afzal Satti

Reputation: 440

I feel that why you are using two different loops for this. You can do this by simply used 1 for of loop as example below:

let comments = [{
    "postId": 6,
    "status": "ACTIVE",
    "id": 28,
    "name": "quo voluptates voluptas nisi veritatis dignissimos dolores ut officiis",
    "email": "[email protected]",
    "body": "voluptatem repellendus quo alias at laudantium\nmollitia quidem esse\ntemporibus consequuntur vitae rerum illum\nid corporis sit id"
  },
  {
    "postId": 6,
    "id": 29,
    "status": "INACTIVE",
    "name": "eum distinctio amet dolor",
    "email": "[email protected]",
    "body": "tempora voluptatem est\nmagnam distinctio autem est dolorem\net ipsa molestiae odit rerum itaque corporis nihil nam\neaque rerum error"
  }
];

const matchedCommentsIds = []
for (const {status, id}of comments) {

  if (status == "ACTIVE" && id){
    matchedCommentsIds.push(id)
  }
}

console.log(matchedCommentsIds)

enter image description here

If we used filter and map functions then the major drawback is that we iterate the array two times, which has no need.

Upvotes: 0

Mamun
Mamun

Reputation: 68923

You can implement map() with Destructing Assignment:

let comments = [{
    "postId": 6,
    "status": "ACTIVE",
    "id": 28,
    "name": "quo voluptates voluptas nisi veritatis dignissimos dolores ut officiis",
    "email": "[email protected]",
    "body": "voluptatem repellendus quo alias at laudantium\nmollitia quidem esse\ntemporibus consequuntur vitae rerum illum\nid corporis sit id"
  },
  {
    "postId": 6,
    "id": 29,
    "status": "INACTIVE",
    "name": "eum distinctio amet dolor",
    "email": "[email protected]",
    "body": "tempora voluptatem est\nmagnam distinctio autem est dolorem\net ipsa molestiae odit rerum itaque corporis nihil nam\neaque rerum error"
  }
];

comments = comments.filter((ob, i) => {
  return ob.status == "ACTIVE" && ob.id
}).map(({id}) => ({id}));

console.log(comments);

Upvotes: 1

tomision
tomision

Reputation: 994

Two solutions:

Use map after filter

let comments = [{
    "postId": 6,
    "status": "ACTIVE",
    "id": 28,
    "name": "quo voluptates voluptas nisi veritatis dignissimos dolores ut officiis",
    "email": "[email protected]",
    "body": "voluptatem repellendus quo alias at laudantium\nmollitia quidem esse\ntemporibus consequuntur vitae rerum illum\nid corporis sit id"
  },
  {
    "postId": 6,
    "id": 29,
    "status": "INACTIVE",
    "name": "eum distinctio amet dolor",
    "email": "[email protected]",
    "body": "tempora voluptatem est\nmagnam distinctio autem est dolorem\net ipsa molestiae odit rerum itaque corporis nihil nam\neaque rerum error"
  }
];

const arr = comments.filter((ob, i) => {
  return ob.status == "ACTIVE"
}).map(ob => ob.id)

Use reduce

let comments = [{
    "postId": 6,
    "status": "ACTIVE",
    "id": 28,
    "name": "quo voluptates voluptas nisi veritatis dignissimos dolores ut officiis",
    "email": "[email protected]",
    "body": "voluptatem repellendus quo alias at laudantium\nmollitia quidem esse\ntemporibus consequuntur vitae rerum illum\nid corporis sit id"
  },
  {
    "postId": 6,
    "id": 29,
    "status": "INACTIVE",
    "name": "eum distinctio amet dolor",
    "email": "[email protected]",
    "body": "tempora voluptatem est\nmagnam distinctio autem est dolorem\net ipsa molestiae odit rerum itaque corporis nihil nam\neaque rerum error"
  }
];

const res = comments.reduce((pre, cur) => {
  if (cur.status === "ACTIVE") {
    return [...pre, cur.id]
  } else {
    return pre
  }
}, [])


I think first one maybe easy to understand.

After is reduce doc: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce

Upvotes: 2

UDID
UDID

Reputation: 2423

You can use map in your code to achieve this. Map

Here is working code.

comments.map(function (data) {
  return data.id
});

This will return the array of id which present in the comments.

Upvotes: 2

Kostya Tresko
Kostya Tresko

Reputation: 854

comments.filter((ob, i) => {
  return ob.status == "ACTIVE" && ob.id
}).map(({id}) => id)

Upvotes: 3

Related Questions