Lodash:如何从对象数组中获取唯一值,然后按这些唯一值对这些对象进行排序?

时间:2019-07-10 00:31:16

标签: javascript lodash

我正在尝试使用Lodash从对象数组中获取唯一值,然后将这些值用作将对象数组排序的键。我能够找到一个解决方案,但是我不确定这是最复杂,易读或性能高效的方法。

使用_.uniq_.map,我能够从每个country获得唯一的artist值。然后,我遍历这些值并通过它们过滤artists

let artists = [
  { name: "Bob Jones", country: "Australia"},
  { name: "Jane Smith", country: "Australia"},
  { name: "James Good", country: "USA"},
  { name: "Jeremy Bond", country: "Japan"},
]

let countries = _.uniq(_.map(artists, 'country'))
// ["Australia", "USA", "Japan"]

let res = []

for (let i = 0; i < countries.length; i++) {
  let country = countries[i]
  let obj = {
    country: country,
    artists: artists.filter(artist => artist.country === country)
  }

  res.push(obj)
}

console.log(res)
/* [
  { country: "Australia",
    artists: [
      { name: "Bob Jones", country: "Australia"},
      { name: "Jane Smith", country: "Australia"}
    ]
  },
  { country: "USA",
    artists: [
      { name: "James Good", country: "USA"}
    ]
  },
  { country: "Japan",
    artists: [
      { name: "Jeremy Bond", country: "Japan"}
    ]
  }
]
*/

我可以使用任何Lodash功能来代替for循环和对象分配吗?

1 个答案:

答案 0 :(得分:3)

使用_.groupBy()将艺术家收集到{ [country]: artists }的对象,然后使用_.map()将对象转换为数组:

const artists = [
  { name: "Bob Jones", country: "Australia"},
  { name: "Jane Smith", country: "Australia"},
  { name: "James Good", country: "USA"},
  { name: "Jeremy Bond", country: "Japan"},
]

const result = _.map(
  _.groupBy(artists, 'country'),
  (artists, country) => ({ country, artists })
)

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>

使用lodash/fp的相同解决方案-使用_.flow()生成函数:

const { flow, groupBy, map, head } = _

const byCountry = flow(
  groupBy('country'),
  map(artists => ({ country: head(artists).country, artists }))
)

const artists = [
  { name: "Bob Jones", country: "Australia"},
  { name: "Jane Smith", country: "Australia"},
  { name: "James Good", country: "USA"},
  { name: "Jeremy Bond", country: "Japan"},
]

const result = byCountry(artists)

console.log(result)
<script src='https://cdn.jsdelivr.net/g/lodash@4(lodash.min.js+lodash.fp.min.js)'></script>