underscore.js

How do I shuffle a Javascript Array ensuring each Index is in a new position in the new Array?

蹲街弑〆低调 提交于 2021-02-04 17:07:12
问题 I have an Array of Objects, like so. var usersGoing = [ { user: 0 }, { user: 1 }, { user: 2 }, { user: 3 }, { user: 4 } ]; I need to shuffle this Array so that no Object remains in the same index as when it was instantiated, like so: [ { user: 3 }, { user: 2 }, { user: 4 }, { user: 0 }, { user: 1 } ] It is IMPERATIVE that the resulting array be sorted in this manner, as each of these user Objects will be assigned to a different user Object. I have tried a few different sorting algorithms,

Filter a json data by another array in underscore.js

耗尽温柔 提交于 2021-02-04 08:40:16
问题 I have a search field and I want to add some complex functionality using underscore.js. Sometimes users search for a whole "sentence" like "Samsung galaxy A20s ultra". I want to filter JSON data using any of the words in the search string and sort by results that contain more of the words. Sample data: var phones = [ {name: "Samsung A10s", id: 845}, {name: "Samsung galaxy", id: 839}, {name: "Nokia 7", id: 814}, {name: "Samsung S20s ultra", id: 514}, {name: "Apple iphone ultra", id: 159},

Sorted Map Implementation

强颜欢笑 提交于 2021-01-27 07:10:38
问题 My Task In my JavaScript code i'm often using objects to "map" keys to values so i can later access them directly through a certain value. For example: var helloMap = {}; helloMap.de = "Hallo"; helloMap["en"] = "Hello"; helloMap.es = "Hola"; So i build up the map object step by step in my source code using the two available notations object style and array style . Later i can then access the values i added through helloMap["de"] for example. So thats all fine if i don't have to care about the

Single Page Applications and Open Graph

我怕爱的太早我们不能终老 提交于 2021-01-26 09:45:08
问题 I'm writing a SPA that uses underscore templating. The app searches for and rates music albums and returns the result via ajax. If facebook open graph metatags cannot be altered dynamically and the url of the page is constant regardless of search result, how can i make it so users can share that they rated a certain album. ie) <meta property="fb:app_id" content="118454308341351" /> <meta property="og:url" content="http://www.appurl.com" /> <meta property="og:title" content="Fleetwood Mac's