2

I want to filter an array of objects based on a key selected. If any of the object has selected: true, I want its ID in return. For e.g. below:

Below is the array I have:

arr = [
 {
    "_id": "6311eb86cc42295428bb663a",
    "name": "Southeast Asia",
    "language": [
        "English"
    ],
    "selected": true
 },
 {
    "_id": "6311eb86cc42295428bb663f",
    "name": "USA",
    "language": [
        "English"
    ],
 },
 {
    "_id": "6311eb86cc42295428bb6635",
    "name": "MENA",
    "language": [
        "English"
    ],
    "selected": true
 }
]

Logic used to get an _id from it:

arr.filter(item => {
   if(item.selected) {
     retrun item._id;
   }
});

Expected output:

[{
  _id: '6311eb86cc42295428bb663a'
}, {
  _id: '6311eb86cc42295428bb6635'
}]

But I got the whole array of object in return instead of just _id.

How can I work around this to get only _id?

6 Answers 6

3

The array.filter method only filters the input array without changing it's content. The callback you're passing in your code is only expected to return true or false. To reshape your result you need to use filter along with array.map

const arr = [
 {
    "_id": "6311eb86cc42295428bb663a",
    "name": "Southeast Asia",
    "language": [
        "English"
    ],
    "selected": true
 },
 {
    "_id": "6311eb86cc42295428bb663f",
    "name": "USA",
    "language": [
        "English"
    ],
 },
 {
    "_id": "6311eb86cc42295428bb6635",
    "name": "MENA",
    "language": [
        "English"
    ],
    "selected": true
 }
];

const result = arr.filter(item => item.selected).map(item => ({_id: item._id}));

console.log(result);

Sign up to request clarification or add additional context in comments.

Comments

1

const data = arr = [
 {
    "_id": "6311eb86cc42295428bb663a",
    "name": "Southeast Asia",
    "language": [
        "English"
    ],
    "selected": true
 },
 {
    "_id": "6311eb86cc42295428bb663f",
    "name": "USA",
    "language": [
        "English"
    ],
 },
 {
    "_id": "6311eb86cc42295428bb6635",
    "name": "MENA",
    "language": [
        "English"
    ],
    "selected": true
 }
]

let result = data.filter(d => d.selected).map(d => {
      let obj ={}
      obj['_id'] = d._id
      return obj
})
console.log(result)

Comments

1

You can use map as below :

let filtered = arr.filter(item => { if(item.selected) { return item._id;}}).map(item => ({_id: item._id}));;

expected output :

 [ { _id: '6311eb86cc42295428bb663a'}, { _id: '6311eb86cc42295428bb6635'}]

1 Comment

Use special code formating
0

Array.filter returns an array based on your function.

So you have to save that variable like this:

let arr2= arr.filter(element => {
    if(element.selected == true)
        return (element)
});

Comments

0

You need to use both filtered and map functions

const filteredArr = arr.filter((element)=>{
    return element.selected != null
})

const reducedArr = filteredArr.map((element) => {
    return {_id:element._id}
})

Comments

0

filter + map == reduce

so this will give you your output:

arr.reduce((acc,val)=>{
  if(val.selected){
    acc.push({_id: val._id})
  }
  return acc
},[])

Or, most likely you actually need just array of values:

arr.reduce((acc,val)=>{
  if(val.selected){
    acc.push(val._id)
  }
  return acc
},[])

2 Comments

Also, reduce will have 1 iteration instead of 2 in case of filter+map

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.