【问题标题】:How to Filter an Array and get the entire data entry?如何过滤数组并获取整个数据条目?
【发布时间】:2021-06-29 21:40:33
【问题描述】:

我有,

[
  {
    _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 = req.params.id,基本上是60673e8a8af58b1fb8b11596。 现在如何使用我拥有的 id 变量过滤数组并获取具有 id 的整个对象?我想要带有 _id、ImageName、ImagePath、__v 和 Description 的整个对象。

我搜索了很多,找到了array.filter方法,但它太复杂了,对我没有帮助,我更加困惑。

【问题讨论】:

    标签: javascript arrays


    【解决方案1】:

    您可以尝试使用Array.prototype.filter()检查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);

    【讨论】:

    • 注意:_id 应该是一个字符串。在最初的问题中不是。
    【解决方案2】:

    假设上面的数组被称为foo;

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

    它的作用是在 foo 数组中查找,并将返回满足回调传递的第一个条件

    但是,如果您想从 mongodb 数据库中获得相同的结果,则可以执行如下查询:

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

    【讨论】:

      【解决方案3】:

      您可以使用Array.prototype.find 来获取元素。它将直接返回元素。如果您需要多种解决方案来查找数组中的元素。 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);

      【讨论】:

        【解决方案4】:

        如果您使用的是猫鼬。您可以使用findById(id)。例如:Model.findById(id)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-01-03
          • 2018-10-04
          • 1970-01-01
          • 2021-12-13
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多