Lodash的uniquby向我讲错了结果

问题描述

在此处输入代码以下数组与api响应一同显示

[{"imgPaths":["gallery/products/11dc67c40ea3812d5f5f8b8585a202f6"],"_id":"5f3e966a65c6d591ba04f3dd","productName":"SBL Arsenic Album 30 CH (30ml) ","categoryId":{"_id":"5f21376c2d46a455487b2ea3","categoryName":"Homeopathy","imgPath":"gallery/category/a7eafa3b8a480dd7d6810dee823104fd"},"manufacturer":"SBL ","basePrice":"85","finalPrice":"77","availability":"in-stock","createdAt":"2020-08-20T15:27:38.484Z"},{"imgPaths":["gallery/products/4d2235903695423f957abb831f6b7266"],"_id":"5f3e967265c6d591ba04f3de","productName":" Dr. Reckeweg Arsenic Album 30 CH (11ml)  ","manufacturer":"Dr. Reckeweg","basePrice":"115","finalPrice":"115","createdAt":"2020-08-20T15:27:46.261Z"},{"imgPaths":["gallery/products/a8fd4422565c7f78fb8d5578e9da2143"],"_id":"5f3e967a65c6d591ba04f3df","productName":"SBL Camphora 1M (1000 CH) (30ml)  ","manufacturer":"SBL","basePrice":"120","finalPrice":"101","createdAt":"2020-08-20T15:27:54.594Z"},{"imgPaths":["gallery/products/1ba84342e40856e71a64bf4b840fe611"],"_id":"5f3e968165c6d591ba04f3e0","productName":"Dr. Reckeweg R89 (Lipocol) (30ml)  ","basePrice":"270","finalPrice":"269","createdAt":"2020-08-20T15:28:01.150Z"},{"imgPaths":["gallery/products/11542fe755e53ff08a983257dbaf12c3"],"_id":"5f3e968a65c6d591ba04f3e1","productName":"SBL Nux Vomica 30 CH (30ml)  ","finalPrice":"80","createdAt":"2020-08-20T15:28:10.468Z"},{"imgPaths":["gallery/products/7ff26dfa8c524e3cc289805819b25bac"],"_id":"5f3eb0eb8861ac98fd52fc8c","productName":"SBL Bryonia Alba 200 CH (30ml)","basePrice":"95","finalPrice":"90","createdAt":"2020-08-20T17:20:43.508Z"}]

我正在尝试使用lodash数组从制造商那里获取唯一值,

const _=require('lodash');
const uniqueArr =_.uniqBy(res.data,'manufacturer');
console.log(uniqueArr.map(item=>item.manufacturer));

并得到如下结果

["SBL ","Dr. Reckeweg","SBL"]

由于空间原因,我们认为第一和第三值不同, 那么我们如何才能从对象值中删除前后空格,以便 这种独特的方法输出如下

["Dr. Reckeweg","SBL"]

解决方法

'manufacturer'_ => _.manufacturer的迭代形式。您可以更改它以使用微调功能。

const _ = require('lodash');
const uniqueArr =_.uniqBy(res.data,item => item.manufacturer.trim());
console.log(uniqueArr.map(item => item.manufacturer.trim()));
,

您只需要先trimmanufacturer

const res = _.chain(data)
  .map((obj) => ({ ...obj,manufacturer: obj.manufacturer.trim() }))
  .uniqBy("manufacturer")
  .map((obj) => obj.manufacturer)
  .value()

完整代码

const data = [
  {
    imgPaths: ["gallery/products/11dc67c40ea3812d5f5f8b8585a202f6"],_id: "5f3e966a65c6d591ba04f3dd",productName: "SBL Arsenic Album 30 CH (30ml) ",categoryId: {
      _id: "5f21376c2d46a455487b2ea3",categoryName: "Homeopathy",imgPath: "gallery/category/a7eafa3b8a480dd7d6810dee823104fd",},manufacturer: "SBL ",basePrice: "85",finalPrice: "77",availability: "in-stock",createdAt: "2020-08-20T15:27:38.484Z",{
    imgPaths: ["gallery/products/4d2235903695423f957abb831f6b7266"],_id: "5f3e967265c6d591ba04f3de",productName: " Dr. Reckeweg Arsenic Album 30 CH (11ml)  ",manufacturer: "Dr. Reckeweg",basePrice: "115",finalPrice: "115",createdAt: "2020-08-20T15:27:46.261Z",{
    imgPaths: ["gallery/products/a8fd4422565c7f78fb8d5578e9da2143"],_id: "5f3e967a65c6d591ba04f3df",productName: "SBL Camphora 1M (1000 CH) (30ml)  ",manufacturer: "SBL",basePrice: "120",finalPrice: "101",createdAt: "2020-08-20T15:27:54.594Z",{
    imgPaths: ["gallery/products/1ba84342e40856e71a64bf4b840fe611"],_id: "5f3e968165c6d591ba04f3e0",productName: "Dr. Reckeweg R89 (Lipocol) (30ml)  ",basePrice: "270",finalPrice: "269",createdAt: "2020-08-20T15:28:01.150Z",{
    imgPaths: ["gallery/products/11542fe755e53ff08a983257dbaf12c3"],_id: "5f3e968a65c6d591ba04f3e1",productName: "SBL Nux Vomica 30 CH (30ml)  ",finalPrice: "80",createdAt: "2020-08-20T15:28:10.468Z",{
    imgPaths: ["gallery/products/7ff26dfa8c524e3cc289805819b25bac"],_id: "5f3eb0eb8861ac98fd52fc8c",productName: "SBL Bryonia Alba 200 CH (30ml)",basePrice: "95",finalPrice: "90",createdAt: "2020-08-20T17:20:43.508Z",]

const res = _.chain(data)
  .map((obj) => ({ ...obj,manufacturer: obj.manufacturer.trim() }))
  .uniqBy("manufacturer")
  .map((obj) => obj.manufacturer)
  .value()

console.log(res)
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.20/lodash.min.js"></script>

,

尝试在map之前做uniq

https://lodash.com/docs/4.17.15#uniqBy

const data = [{"imgPaths":["gallery/products/11dc67c40ea3812d5f5f8b8585a202f6"],"_id":"5f3e966a65c6d591ba04f3dd","productName":"SBL Arsenic Album 30 CH (30ml) ","categoryId":{"_id":"5f21376c2d46a455487b2ea3","categoryName":"Homeopathy","imgPath":"gallery/category/a7eafa3b8a480dd7d6810dee823104fd"},"manufacturer":"SBL ","basePrice":"85","finalPrice":"77","availability":"in-stock","createdAt":"2020-08-20T15:27:38.484Z"},{"imgPaths":["gallery/products/4d2235903695423f957abb831f6b7266"],"_id":"5f3e967265c6d591ba04f3de","productName":" Dr. Reckeweg Arsenic Album 30 CH (11ml)  ","manufacturer":"Dr. Reckeweg","basePrice":"115","finalPrice":"115","createdAt":"2020-08-20T15:27:46.261Z"},{"imgPaths":["gallery/products/a8fd4422565c7f78fb8d5578e9da2143"],"_id":"5f3e967a65c6d591ba04f3df","productName":"SBL Camphora 1M (1000 CH) (30ml)  ","manufacturer":"SBL","basePrice":"120","finalPrice":"101","createdAt":"2020-08-20T15:27:54.594Z"},{"imgPaths":["gallery/products/1ba84342e40856e71a64bf4b840fe611"],"_id":"5f3e968165c6d591ba04f3e0","productName":"Dr. Reckeweg R89 (Lipocol) (30ml)  ","basePrice":"270","finalPrice":"269","createdAt":"2020-08-20T15:28:01.150Z"},{"imgPaths":["gallery/products/11542fe755e53ff08a983257dbaf12c3"],"_id":"5f3e968a65c6d591ba04f3e1","productName":"SBL Nux Vomica 30 CH (30ml)  ","finalPrice":"80","createdAt":"2020-08-20T15:28:10.468Z"},{"imgPaths":["gallery/products/7ff26dfa8c524e3cc289805819b25bac"],"_id":"5f3eb0eb8861ac98fd52fc8c","productName":"SBL Bryonia Alba 200 CH (30ml)","basePrice":"95","finalPrice":"90","createdAt":"2020-08-20T17:20:43.508Z"}]

const uniqueArr = _.uniqBy(_.map(data,'manufacturer'),_.trim);

console.log(uniqueArr);
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.20/lodash.min.js"></script>