javascript - filter an array of object based on a key and get only key value - Stack Overflow

I want to filter an array of objects based on a key selected. If any of the object has selected: true,

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?

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?

Share Improve this question asked Oct 6, 2022 at 10:08 Prshant SharmaPrshant Sharma 1191 gold badge5 silver badges11 bronze badges
Add a ment  | 

6 Answers 6

Reset to default 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);

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)

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

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

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

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
},[])

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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信