如何使用_underscoreJS从字符串对象数组中删除重复项

时间:2021-06-03 16:54:20

标签: javascript arrays json filter underscore.js

我有这张桌子

[
    "PC",
    "PS3,Xbox 360,PC,PS4,Xbox One,Nintendo Switch",
    "PC,PS4,Xbox One,Nintendo Switch",
    "PS4,PC",
    "PS4,PS5,Xbox One,Nintendo Switch,PC",
    "PC,PS4,Xbox One",
    "PC,Android"
]

我想删除重复的项目并得到这个结果:

["PS3,Xbox 360,PC,PS4,Xbox One,Nintendo Switch, Android"]

我用下划线 js 尝试了这个,但它给出了空数组:

 data = _.chain(games)
        .groupBy('platforms')
        .map((value, key) => { return key })
        
    
    return data

json 数据:https://jsoneditoronline.org/#left=cloud.3b82169327044c04b7207fa186aee85b&right=local.tiniqu

4 个答案:

答案 0 :(得分:1)

const a = [
  'PC',
  'PS3,Xbox 360,PC,PS4,Xbox One,Nintendo Switch',
  'PC,PS4,Xbox One,Nintendo Switch',
  'PS4,PC',
  'PS4,PS5,Xbox One,Nintendo Switch,PC',
  'PC,PS4,Xbox One',
  'PC,Android'
];

const result = _.chain(a)
  .map(s => s.split(','))
  .flatten()
  .uniq()
  .value()
  .join(',');

console.log([result]);
<script src="https://cdn.jsdelivr.net/npm/underscore@1.13.1/underscore-umd-min.js"></script>

答案 1 :(得分:1)

您可以使用 Array.map 对数组进行分组,使用 Array.flat 对数组进行边距调整,以及使用 Set 删除重复项。

let input1 = ["PC", "PS3,Xbox 360,PC,PS4,Xbox One,Nintendo Switch", "PC,PS4,Xbox One,Nintendo Switch", "PS4,PC", "PS4,PS5,Xbox One,Nintendo Switch,PC", "PC,PS4,Xbox One", "PC,Android"];
let input2 = { "data": [{ "userId": 8, "game": "League of legends", "playTime": 500, "genre": "MOBA", "platforms": ["PC"] }, { "userId": 7, "game": "World of warcraft", "playTime": 1500, "genre": "MMORPG", "platforms": ["PC"] }, { "userId": 88, "game": "Dark Souls", "playTime": 109, "genre": "Action RPG", "platforms": ["PS3", "Xbox 360", "PC", "PS4", "Xbox One", "Nintendo Switch"] }, { "userId": 88, "game": "The Witcher 3: Wild Hunt", "playTime": 9, "genre": "RPG", "platforms": ["PC", "PS4", "Xbox One", "Nintendo Switch"] }, { "userId": 1, "game": "The last of us 2", "playTime": 100, "genre": "FPS", "platforms": ["PS4", "PC"] }, { "userId": 7, "game": "Hitman 3", "playTime": 60, "genre": "Stealth", "platforms": ["PS4", "PS5", "Xbox One", "Nintendo Switch", "PC"] }, { "userId": 99, "game": "Minecraft", "playTime": 1002, "genre": "Sandbox", "platforms": ["PC"] }, { "userId": 7, "game": "Hearthstone", "playTime": 1000, "genre": "Card Game", "platforms": ["PC"] }, { "userId": 7, "game": "FIFA", "playTime": 2000, "genre": "Sport", "platforms": ["PC", "PS4", "Xbox One"] }, { "userId": 2, "game": "The Witcher 3: Wild Hunt", "playTime": 78, "genre": "RPG", "platforms": ["PC", "PS4", "Xbox One", "Nintendo Switch"] }, { "userId": 47, "game": "League of legends", "playTime": 850, "genre": "MOBA", "platforms": ["PC"] }, { "userId": 2, "game": "Among Us", "playTime": 5000, "genre": "Multiplayer", "platforms": ["PC", "Android"] }, { "userId": 2, "game": "Valorant", "playTime": 2000, "genre": "FPS", "platforms": ["PC"] }, { "userId": 9, "game": "Valorant", "playTime": 80, "genre": "FPS", "platforms": ["PC"] }, { "userId": 9, "game": "Dark Souls", "playTime": 109, "genre": "RPG", "platforms": ["PS3", "Xbox 360", "PC", "PS4", "Xbox One", "Nintendo Switch"] }, { "userId": 9, "game": "The Witcher 3: Wild Hunt", "playTime": 900, "genre": "RPG", "platforms": ["PC", "PS4", "Xbox One", "Nintendo Switch"] }, { "userId": 24, "game": "League of legends", "playTime": 300, "genre": "MOBA", "platforms": ["PC"] }, { "userId": 24, "game": "World of warcraft", "playTime": 800, "genre": "MMORPG", "platforms": ["PC"] }, { "userId": 54, "game": "Minecraft", "playTime": 231, "genre": "Sandbox", "platforms": ["PC"] }, { "userId": 7, "game": "Minecraft", "playTime": 777, "genre": "Sandbox", "platforms": ["PC"] }, { "userId": 7, "game": "Hitman 3", "playTime": 90, "genre": "Stealth", "platforms": ["PS4", "PS5", "Xbox One", "Nintendo Switch", "PC"] }] }

let removeDuplicate = (array, procedure) => [...new Set(array.map(procedure).flat())];

let result1 = removeDuplicate(input1, item => item.split(","));
let result2 = removeDuplicate(input2.data, ({ platforms }) => platforms);

console.log({ result1, result2 });

答案 2 :(得分:0)

我相信首先要做的就是像这样规范化你的数组......

const array = [
    "PC",
    "PS3,Xbox 360,PC,PS4,Xbox One,Nintendo Switch",
    "PC,PS4,Xbox One,Nintendo Switch",
    "PS4,PC",
    "PS4,PS5,Xbox One,Nintendo Switch,PC",
    "PC,PS4,Xbox One",
    "PC,Android"
]

let arrayNormalized = []

array.every(item => {
  const arrayFromItem = item.split(',')
  arrayNormalizad = [...arrayNormalizad, ...arrayFromItem]
})

之后,你可以使用_js中的函数uniq

arrayNormalized = _.uniq(arrayNormalizad)

答案 3 :(得分:0)

您不需要使用下划线或任何函数式数组方法来完成此操作

Array.from(new Set(array.join(',').split(',')));
// ["PC", "PS3", "Xbox 360", "PS4", "Xbox One", "Nintendo Switch", "PS5", "Android"]

此代码

  1. 使用 , 分隔符连接所有行
  2. 然后通过 ,
  3. 将所有内容拆分为一系列标题
  4. 将所有内容放在 Set 下删除重复项
  5. Array.from 将集合数据移回数组