547n00n
547n00n

Reputation: 1546

How to remove duplicate item form array of string objects with _underscoreJS

I have this table

[
    "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"
]

I want to remove duplicated item and get this result :

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

I tried this with underscore js but it given empty array :

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

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

Upvotes: 0

Views: 174

Answers (4)

svarog
svarog

Reputation: 9837

You don't need to use underscore nor any functional array methods to accomplish this

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

This code

  1. joins all the lines using the , delimiter
  2. then splits everything into an array of titles by ,
  3. placing everything under Set removed duplicates
  4. Array.from moves the set data back into an Array

Upvotes: 0

Nur
Nur

Reputation: 2473

You can use Array.map to groups arrays, Array.flat to marge arrays and Set to remove duplicate.

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 });

Upvotes: 1

Pablo Trindade
Pablo Trindade

Reputation: 41

I believe that the first thing to do is normalize you array like this...

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]
})

After that, you can use a function uniq from _js

arrayNormalized = _.uniq(arrayNormalizad)

Upvotes: 0

aleksxor
aleksxor

Reputation: 8370

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/[email protected]/underscore-umd-min.js"></script>

Upvotes: 1

Related Questions