1

Based on a quick mockup like below I need to add URL route objects to an Array property in my app.

in the function below addAppRoute(routeObbj) I would like to add a check to see if the new route being added already exist or not in the property this.cache.webDevAppRoutes

How can I make sure duplicates are not added? It would need to be based on a duplicate module and action being the same

var app = {
    cache: {
        webDevAppRoutes: [],
    },

    addAppRoute: function(route) {
        var routes = this.cache.webDevAppRoutes;
        routes.push(route);
    },

    getAppRoutes: function() {
        var routes = this.cache.webDevAppRoutes;
        console.log(routes);
    },

    addBookmarkAppRoutes: function() {

        this.addAppRoute({
            module: 'bookmarks',
            action: 'view',
            params: [{
                id: 123
            }],
        });

        this.addAppRoute({
            module: 'bookmarks',
            action: 'edit',
            params: [{
                id: 123
            }],
        });

        this.addAppRoute({
            module: 'bookmarks',
            action: 'add',
            params: [],
        });
    },

}

2 Answers 2

2

Just loop through the array and check that it doesn't already have an entry with that module and action. You can use Array#some, for instance:

if (routes.some(function(r) { return r.module == route.module && r.action == route.action)) {
    // It's a duplicate
}

Array#some returns true as soon as a call to its callback returns a truthy value; otherwise, it returns false.

In ES2015, that's a bit more concise:

if (routes.some(r => r.module == route.module && r.action == route.action)) {
    // It's a duplicate
}
Sign up to request clarification or add additional context in comments.

Comments

1

Just have a Map of already added routes:

var webDevAppRoutes = new Map();


function addAppRoute(route) {
    var key = [route.module, route.action].join();

    if(!webDevAppRoutes.has(key))
        webDevAppRoutes.set(key, route);
}

addAppRoute({
    module: 'bookmarks',
    action: 'view',
    params: [{
        id: 123
    }],
});

addAppRoute({
    module: 'bookmarks',
    action: 'edit',
    params: [{
        id: 123
    }],
});

addAppRoute({
    module: 'bookmarks',
    action: 'add',
    params: [],
});

addAppRoute({
    module: 'bookmarks',
    action: 'add',
    params: 'once again',
});

console.log([...webDevAppRoutes.values()])

For ES5, find a polyfill or use a POJO instead of Map.

2 Comments

This is interesting and nice to see a new feature example usage. I notice that var webDevAppRoutes no longer behaves like an array of objects. Is there a way to use this to prevent duplicate routes being added to my array while still allowing me to access my object property as an array of objects?
@JasonDavis: look at the last line: map.values() gives you an iterator, and [...map.values()] is a normal array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.