見出し画像

tidyjsを使った重複排除のやり方について

こんにちわ。nap5です。

tidyjsを使った重複排除のやり方について紹介したいと思います。


SQLRと同じ感覚で調べれば、欲しいメソッドにたどり着ける感じです。


重複排除となると以下のような感じでアイテムが非オブジェクトの場合は重複排除できると思いますが、アイテムがオブジェクトの場合はバニラでやると大変になってきます。

[...new Set(items)]


そこで、tidyjsのdistinctメソッドを使います。

今回デモを用意してみました。

テストデータです。

[
  {"location": "Portugal\n·\nAlentejo"},
  {"location": "Portugal\n·\nDouro"},
  {"location": "Portugal\n·\nDouro"},
  {"location": "Portugal\n·\nDouro"},
  {"location": "Portugal\n·\nMadeira"},
  {"location": "United States\n·\nCalifornia"},
  {"location": "United States\n·\nDiamond Mountain District"},
  {"location": "United States\n·\nHowell Mountain"},
  {"location": "United States\n·\nHowell Mountain"},
  {"location": "United States\n·\nHowell Mountain"},
  {"location": "United States\n·\nMoon Mountain District"},
  {"location": "United States\n·\nNapa Valley"},
  {"location": "United States\n·\nNapa Valley"},
  {"location": "United States\n·\nNapa Valley"},
  {"location": "United States\n·\nNapa Valley"},
  {"location": "United States\n·\nNapa Valley"},
  {"location": "United States\n·\nNapa Valley"},
  {"location": "United States\n·\nNapa Valley"},
  {"location": "United States\n·\nNapa Valley"},
  {"location": "United States\n·\nNapa Valley"},
  {"location": "United States\n·\nNapa Valley"},
  {"location": "United States\n·\nNapa Valley"},
  {"location": "United States\n·\nNapa Valley"},
  {"location": "United States\n·\nNapa Valley"},
  {"location": "United States\n·\nNapa Valley"},
  {"location": "United States\n·\nOakville"},
  {"location": "United States\n·\nOakville"},
  {"location": "United States\n·\nOakville"},
  {"location": "United States\n·\nOakville"},
  {"location": "United States\n·\nOakville"},
  {"location": "United States\n·\nOakville"},
  {"location": "United States\n·\nOakville"},
  {"location": "United States\n·\nRussian River Valley"},
  {"location": "United States\n·\nRutherford"},
  {"location": "United States\n·\nRutherford"},
  {"location": "United States\n·\nRutherford"},
  {"location": "United States\n·\nRutherford"},
  {"location": "United States\n·\nRutherford"},
  {"location": "United States\n·\nRutherford"},
  {"location": "United States\n·\nSonoma Mountain"},
  {"location": "United States\n·\nSt. Helena"},
  {"location": "United States\n·\nSt. Helena"},
  {"location": "United States\n·\nSta. Rita Hills"},
  {"location": "United States\n·\nYamhill County"}
]


デモコードです。

import testData from '../data/test.json';
import {arrange, asc, distinct, filter, map, tidy} from '@tidyjs/tidy';

const niceData = tidy(
  testData,
  filter((item) => {
    return item.location !== '';
  }),
  map((item) => {
    const [a, b] = item.location.split(/·/);
    return {location: a.replace(/\n/g, ''), subLocation: b.replace(/\n/g, '')};
  }),
  distinct(['location', 'subLocation'])
);

console.log(niceData);


実行結果です。

[
  {"location": "Portugal", "subLocation": "Alentejo"},
  {"location": "Portugal", "subLocation": "Douro"},
  {"location": "Portugal", "subLocation": "Madeira"},
  {"location": "United States", "subLocation": "California"},
  {"location": "United States", "subLocation": "Diamond Mountain District"},
  {"location": "United States", "subLocation": "Howell Mountain"},
  {"location": "United States", "subLocation": "Moon Mountain District"},
  {"location": "United States", "subLocation": "Napa Valley"},
  {"location": "United States", "subLocation": "Oakville"},
  {"location": "United States", "subLocation": "Russian River Valley"},
  {"location": "United States", "subLocation": "Rutherford"},
  {"location": "United States", "subLocation": "Sonoma Mountain"},
  {"location": "United States", "subLocation": "St. Helena"},
  {"location": "United States", "subLocation": "Sta. Rita Hills"},
  {"location": "United States", "subLocation": "Yamhill County"}
]


ランキング形式など実装するときにtidyjsは便利です。他にも重宝する場面が多々あります。

簡単ですが、以上です。

この記事が気に入ったらサポートをしてみませんか?