2

I have,

[
  {
    _id: 60673d40ba14f617081bdf4d,
    ImageName: 'Flowers',
    ImagePath: './public/assets/img/1.jpg',
    Description: 'A cool Image',
    __v: 0
  },
  {
    _id: 60673e099d76ef16a8ec4588,
    ImageName: 'Water Bubble Colors',
    ImagePath: './public/assets/img/2.jpg',
    Description: 'Wallpapers',
    __v: 0
  },
  {
    _id: 60673e39f10c4a12a0f4ea31,
    ImageName: 'Egg Silhouette Wallpaper',
    ImagePath: './public/assets/img/3.jpg',
    Description: 'Wallpapers',
    __v: 0
  },
  {
    _id: 60673e4a15d9bb18a01312df,
    ImageName: 'Pencil BW ',
    ImagePath: './public/assets/img/4.jpg',
    Description: 'Wallpapers',
    __v: 0
  },
  {
    _id: 60673e6c6de9bb1bf8c65d25,
    ImageName: 'Water',
    ImagePath: './public/assets/img/5.jpg',
    Description: 'Wallpapers',
    __v: 0
  },
  {
    _id: 60673e8a8af58b1fb8b11596,
    ImageName: 'Dining Plate',
    ImagePath: './public/assets/img/6.jpg',
    Description: 'Wallpapers',
    __v: 0
  }
]

and var id = req.params.id which basically is 60673e8a8af58b1fb8b11596. Now how can I filter the array with the id variable I have and get the entire Object which has the id? I want the entire object with _id,ImageName, ImagePath, __v and Description.

I searched a lot,found array.filter method, but its so complexed and didnt help me and i got even more confused.

E_net4
  • 27,810
  • 13
  • 101
  • 139
MasterMind
  • 884
  • 8
  • 21

4 Answers4

4

You can try using Array.prototype.filter() checking the id:

var data = [
  {
    _id: '60673d40ba14f617081bdf4d',
    ImageName: 'Flowers',
    ImagePath: './public/assets/img/1.jpg',
    Description: 'A cool Image',
    __v: 0
  },
  {
    _id: '60673e099d76ef16a8ec4588',
    ImageName: 'Water Bubble Colors',
    ImagePath: './public/assets/img/2.jpg',
    Description: 'Wallpapers',
    __v: 0
  },
  {
    _id: '60673e39f10c4a12a0f4ea31',
    ImageName: 'Egg Silhouette Wallpaper',
    ImagePath: './public/assets/img/3.jpg',
    Description: 'Wallpapers',
    __v: 0
  },
  {
    _id: '60673e4a15d9bb18a01312df',
    ImageName: 'Pencil BW ',
    ImagePath: './public/assets/img/4.jpg',
    Description: 'Wallpapers',
    __v: 0
  },
  {
    _id: '60673e6c6de9bb1bf8c65d25',
    ImageName: 'Water',
    ImagePath: './public/assets/img/5.jpg',
    Description: 'Wallpapers',
    __v: 0
  },
  {
    _id: '60673e8a8af58b1fb8b11596',
    ImageName: 'Dining Plate',
    ImagePath: './public/assets/img/6.jpg',
    Description: 'Wallpapers',
    __v: 0
  }
]
var id = '60673e8a8af58b1fb8b11596'; //req.params.id

var res = data.filter(d => d._id == id);
console.log(res);
Mamun
  • 66,969
  • 9
  • 47
  • 59
2

SO lets say the above array is called foo ;

let item = foo.find(e=>e._id == your_id);

What this does is looking in the foo array and will return the first condition that satisfies the callback passed

However if you wanted to get the same result from the mongodb database instead you could perform a query like this:

collection.find({_id:ObjectID(your_id)})

Manos Kounelakis
  • 2,848
  • 5
  • 31
  • 55
2

You can use Array.prototype.find to get the element. It will directly return the element. If you need multiple solution to find the Element in array. Check if object value exists within a Javascript array of objects and if not add a new object to array

const arr = [
  {
    _id: "60673d40ba14f617081bdf4d",
    ImageName: "Flowers",
    ImagePath: "./public/assets/img/1.jpg",
    Description: "A cool Image",
    __v: 0,
  },
  {
    _id: "60673e099d76ef16a8ec4588",
    ImageName: "Water Bubble Colors",
    ImagePath: "./public/assets/img/2.jpg",
    Description: "Wallpapers",
    __v: 0,
  },
  {
    _id: "60673e39f10c4a12a0f4ea31",
    ImageName: "Egg Silhouette Wallpaper",
    ImagePath: "./public/assets/img/3.jpg",
    Description: "Wallpapers",
    __v: 0,
  },
  {
    _id: "60673e4a15d9bb18a01312df",
    ImageName: "Pencil BW ",
    ImagePath: "./public/assets/img/4.jpg",
    Description: "Wallpapers",
    __v: 0,
  },
  {
    _id: "60673e6c6de9bb1bf8c65d25",
    ImageName: "Water",
    ImagePath: "./public/assets/img/5.jpg",
    Description: "Wallpapers",
    __v: 0,
  },
  {
    _id: "60673e8a8af58b1fb8b11596",
    ImageName: "Dining Plate",
    ImagePath: "./public/assets/img/6.jpg",
    Description: "Wallpapers",
    __v: 0,
  },
];

var id = "60673e8a8af58b1fb8b11596";

const result = arr.find(({ _id }) => _id === id);
console.log(result);
DecPK
  • 24,537
  • 6
  • 26
  • 42
1

If you are using mongoose. You can use findById(id). For ex: Model.findById(id)

Som Shekhar Mukherjee
  • 4,701
  • 1
  • 12
  • 28