Home > Back-end >  filter an array of object based on a key and get only key value
filter an array of object based on a key and get only key value

Time:10-06

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?

CodePudding user response:

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

CodePudding user response:

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)

CodePudding user response:

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

CodePudding user response:

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}
})

CodePudding user response:

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'}]
  • Related