function adding space to array item

Rikard

I have a function to filter CSS classes for duplicates and empty spaces, but my function exports arrays with empty spaces anyway...

var classes = function (className, current) {
    var classNames = [className], uniques = {};
    if(current) classNames = classNames.concat(current.split(' '));
    return classNames.filter(function (className,i) {
        if(className == "") return;
        className = className.match(/\S+/)[0];
       if (!uniques[className]) return uniques[className] = className;
    });
};

When I run classes(' foo') I get [" foo"] with a empty space anyway, although console.log(' foo'.match(/\S+/)[0]; returns 'foo' with no space.

What am I missing?

Fiddle

mu is too short

You misunderstand how filter works. From the fine manual:

The filter() method creates a new array with all elements that pass the test implemented by the provided function.

The return value from the callback tells filter which elements of classNames should be passed through, the return value is merely tested for truthiness. The return value of the callback isn't returned by filter, the original untouched values in classNames are. So the problem isn't that an extra space is being added, the problem is that nothing is removing the spaces.

If you want to test and modify at the same time then reduce will serve you better:

return classNames.reduce(function(a, className) {
    if(!className)
        return a;
    className = className.match(/\S+/)[0];
    if(!uniques[className]) {
        a.push(className);
        uniques[className] = className;
    }
    return a;
}, []);

Note all the return a; statements and the lack of return;s.

Demo: http://jsfiddle.net/ambiguous/bw3JS/

You could also use map to clean up the spaces and then filter to clean up the undefineds with something like this:

return classNames.map(function(className) {
    if(!className)
        return;
    className = className.match(/\S+/)[0];
    if(!uniques[className])
        return uniques[className] = className;
}).filter(function(className) {
    return typeof className !== 'undefined';
});

Demo: http://jsfiddle.net/ambiguous/8j5Bb/

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

function adding space to array item

From Dev

Adding space in an array in JavaScript

From Dev

Adding item to array once when function is called multiple times?

From Dev

Reference is adding item to array

From Dev

Reference is adding item to array

From Dev

Adding item on array in javascript

From Dev

Adding space between Strings in Array

From Dev

Adding JSON array to DynamoDB Item

From Dev

PHP Function for Item in Array

From Dev

Python,join() function, adding space between words

From Dev

repeater is not updated after adding item to array

From Dev

python loop in array adding individual item prices

From Dev

Adding an element to every item of an array in ruby

From Dev

MongoDB > Adding a new item into an object inside an array

From Dev

Why is it subtracting an Item in the array instead of adding it?

From Dev

Adding function to Json Array in AngularJS

From Dev

PowerShell function for adding elements to an array

From Dev

Pass an array item to a function in PowerShell

From Dev

array.push(item) in function

From Dev

Adding element to array in function, but array is still empty

From Dev

Split command in Excel VBA adding a space between every character in array

From Dev

Swift: Print separator not adding a space between array items

From Java

Adding unknown amount of objects to Array via function

From Dev

Adding function handlers in cell array in MATLAB

From Dev

Adding values form array to a mathematical function

From Dev

Adding a reduced array to a variable inside of a function?(js)

From Dev

Update a JsObject by adding an item to one of its branches holding an array of numbers

From Dev

Time complexity of adding a new item to the back of a array-based list

From Dev

Sometimes Array isn't adding item from multiple threads

Related Related

  1. 1

    function adding space to array item

  2. 2

    Adding space in an array in JavaScript

  3. 3

    Adding item to array once when function is called multiple times?

  4. 4

    Reference is adding item to array

  5. 5

    Reference is adding item to array

  6. 6

    Adding item on array in javascript

  7. 7

    Adding space between Strings in Array

  8. 8

    Adding JSON array to DynamoDB Item

  9. 9

    PHP Function for Item in Array

  10. 10

    Python,join() function, adding space between words

  11. 11

    repeater is not updated after adding item to array

  12. 12

    python loop in array adding individual item prices

  13. 13

    Adding an element to every item of an array in ruby

  14. 14

    MongoDB > Adding a new item into an object inside an array

  15. 15

    Why is it subtracting an Item in the array instead of adding it?

  16. 16

    Adding function to Json Array in AngularJS

  17. 17

    PowerShell function for adding elements to an array

  18. 18

    Pass an array item to a function in PowerShell

  19. 19

    array.push(item) in function

  20. 20

    Adding element to array in function, but array is still empty

  21. 21

    Split command in Excel VBA adding a space between every character in array

  22. 22

    Swift: Print separator not adding a space between array items

  23. 23

    Adding unknown amount of objects to Array via function

  24. 24

    Adding function handlers in cell array in MATLAB

  25. 25

    Adding values form array to a mathematical function

  26. 26

    Adding a reduced array to a variable inside of a function?(js)

  27. 27

    Update a JsObject by adding an item to one of its branches holding an array of numbers

  28. 28

    Time complexity of adding a new item to the back of a array-based list

  29. 29

    Sometimes Array isn't adding item from multiple threads

HotTag

Archive