skip to Main Content

Current result has this:

[
['Cycling','Chess'],
['Cycling','Pilates'],
['Cycling','Elevate'],
['Cycling','Kundalini'],
['Cycling','Scrabble'],
['Cycling','Crossword'],
['Cycling','Darning'],
['Running','Ashtanga'],
['Running','Poker'],
['Running','Sudoku'],
['Running','Sewing'],
['Running','Jump'],
['Boxing','Mushin'],
['Boxing','Tai'],
['Boxing','Tae'],
['Boxing','1000'],
['Boxing','Transient'],
['Boxing','Walking'],
['Strength','Flow'],
['Strength','Tantric'],
['Strength','Quigong'],
['Strength','Active']
]

Split the array so that only the following remains:

['Chess'],
['Pilates'],
['Elevate'],
['Kundalini'],
['Scrabble'],
['Crossword'],
['Darning'],
['Ashtanga'],
['Poker'],
['Sudoku'],
['Sewing'],
['Jump'],
['Mushin'],
['Tai'],
['Tae'],
['1000'],
['Transient'],
['Walking'],
['Flow'],
['Tantric'],
['Quigong'],
['Active']

Split only appears to work on 1 dimension arrays so I’m looking for an alternative. Numpty has an hsplit function but unable to find an equivalent for Javascript. Thanks

3

Answers


  1. Use map: arr.map(subarr => [subarr[1]]), though I don’t see why you want one-element lists. arr.map(subarr => subarr[1]), producing a list of strings, is probably more useful.

    const arr = [
      ['Cycling','Chess'],
      ['Cycling','Pilates'],
      ['Cycling','Elevate'],
      ['Cycling','Kundalini'],
      ['Cycling','Scrabble'],
      ['Cycling','Crossword'],
      ['Cycling','Darning'],
      ['Running','Ashtanga'],
      ['Running','Poker'],
      ['Running','Sudoku'],
      ['Running','Sewing'],
      ['Running','Jump'],
      ['Boxing','Mushin'],
      ['Boxing','Tai'],
      ['Boxing','Tae'],
      ['Boxing','1000'],
      ['Boxing','Transient'],
      ['Boxing','Walking'],
      ['Strength','Flow'],
      ['Strength','Tantric'],
      ['Strength','Quigong'],
      ['Strength','Active']
    ];
    console.log(arr.map(subarr => [subarr[1]]));
    Login or Signup to reply.
  2. you can use the map method, here’s sample code:

    const arr = [
    ['Cycling','Chess'],
    ['Cycling','Pilates'],
    ['Cycling','Elevate'],
    ['Cycling','Kundalini'],
    ['Cycling','Scrabble'],
    ['Cycling','Crossword'],
    ['Cycling','Darning'],
    ['Running','Ashtanga'],
    ['Running','Poker'],
    ['Running','Sudoku'],
    ['Running','Sewing'],
    ['Running','Jump'],
    ['Boxing','Mushin'],
    ['Boxing','Tai'],
    ['Boxing','Tae'],
    ['Boxing','1000'],
    ['Boxing','Transient'],
    ['Boxing','Walking'],
    ['Strength','Flow'],
    ['Strength','Tantric'],
    ['Strength','Quigong'],
    ['Strength','Active']
    ];
    
    const new_arr = arr.map( element => {
    return [element[1]];
    });
    
    console.log(new_arr);
    Login or Signup to reply.
  3. You can use reduce to create a dictionary, so you "deduplicate" data on the same iteration. This code only works for arrays with 2 items. You can easily iterate over that array so you can use it with arrays of any size

    var arr = [
    ['Cycling','Chess'],
    ['Cycling','Pilates'],
    ['Cycling','Elevate'],
    ['Cycling','Kundalini'],
    ['Cycling','Scrabble'],
    ['Cycling','Crossword'],
    ['Cycling','Darning'],
    ['Running','Ashtanga'],
    ['Running','Poker'],
    ['Running','Sudoku'],
    ['Running','Sewing'],
    ['Running','Jump'],
    ['Boxing','Mushin'],
    ['Boxing','Tai'],
    ['Boxing','Tae'],
    ['Boxing','1000'],
    ['Boxing','Transient'],
    ['Boxing','Walking'],
    ['Strength','Flow'],
    ['Strength','Tantric'],
    ['Strength','Quigong'],
    ['Strength','Active']
    ]
    
    const data = Object.keys(arr.reduce((prev, act) => {
      prev[act[0]] = 1
      prev[act[1]] = 1
      return prev
    }, {}))
    
    console.log(data)
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search