flashpunk
flashpunk

Reputation: 772

Underscore _.findWhere contains backslash

I'm trying to use _.findWhere to locate nodes that contain backslashes (they are indicating a 'sub folder' in an api). However i'm having an issue searching for a single backslash, and i'm not sure if this is just one of those things with the language that isn't easily possible. My function call currently looks like this:

        for(var i in data){
            if(
                _.findWhere(data, {name:'\\'})
            ) {
                console.log(data[i]);
                console.log('this');
            };
        }

and an example of the data:

[
    {
        "name": "QA",
        "matchedElements": [
            {
                "id": 285,
                "name": " exch2010"
            },
            {
                "id": 288,
                "name": "ad01"
            },
            {
                "id": 289,
                "name": "exchange01"
            },
            {
                "id": 290,
                "name": "ad02"
            }
        ]
    },
    {
        "name": "QA\\Stuff",
        "matchedElements": [
            {
                "id": 240,
                "name": "build-vmstudio-26"
            },
            {
                "id": 241,
                "name": "build-seleniumw"
            },
            {
                "id": 242,
                "name": "build-linux-02"
            },
            {
                "id": 250,
                "name": "build-rh5-x64"
            },
            {
                "id": 251,
                "name": "build-rh5-x86"
            },
            {
                "id": 563,
                "name": "build-linux-01"
            }
        ]
    },
    {
        "name": "PROD",
        "matchedElements": [
            {
                "id": 7,
                "name": "css-ora11sql2k8.uptimesoftware.com"
            },
            {
                "id": 8,
                "name": "css-sql2005.uptimesoftware.com"
            }
        ]
    }
]

Eventually the script will take the node with the double back slash, match it with a node of the same name and place it as a sub node to create a nested object.

Upvotes: 0

Views: 1086

Answers (1)

Bergi
Bergi

Reputation: 664464

findWhere is looking for an exact match - but none of your data is named only with the backslash. Instead, you would use find to get the first element with that name. However, the function you're actually looking for is _.some:

for (var i=0; i<data.length; i++){
    if( _.some(data[i].matchedElements, function(element) {
            return /\\/.test(element.name);
    }) ) {
        console.log(data[i]);
    }
}

Also, do not use for in loops on arrays!


Actually, you don't seem to need that loop at all. If you want Underscore to find those elements in data whose .name contains a backslash, just use

var elements = _.filter(data, function(element) {
    return /\\/.test(element.name);
});
_.each(elements, function(el) {
    console.log(el);
});

or equivalent - completely without underscore - the loop structure that you already had:

for (var i=0; i<data.length; i++){
    if( /\\/.test(data[i].name) ) {
        console.log(data[i]);
    }
}

Instead of /\\/.test(x) you also could use x.indexOf("\\")!=-1.

Upvotes: 2

Related Questions