56177

Dustjs Display Unique Values Only?

Lets say i have the following JSON

{ names: ["John", "Peter", "Ron", "John", "James", "John"] }

I need DustJS to render the following names

John Peter Ron James

Notice that these are unique values in an array. Any ideas? Thank you so much!

Answer1:

This can be done using a common algorithm to 'unique' an array:

Array.prototype.getUnique = function(){ var u = {}, a = []; for(var i = 0, l = this.length; i < l; ++i){ if(u.hasOwnProperty(this[i])) { continue; } a.push(this[i]); u[this[i]] = 1; } return a; }

It's done by taking the values, attempting to add them as keys to an object (which will only work if they're different). If success, it adds that key to an array. If fail, it ignores the key. It then returns the array. I have a working dust.js demo here:

Working Demo

Answer2:

I believe this will generate your "also acceptable" form:

{#options}] {.} {#variants} {.options[$idx]} {/variants} {/options}

Recommend

  • How to produce a unique number from combination of 2 numbers (in any order) in PHP? [closed]
  • freed script with hasOwnProperty
  • Dynamically calling a static method
  • Dustjs Display Unique Values Only?
  • Prevent reloading page after submiting form. ( no ajax )
  • Is there any way to distinguish between an unset property and a property set to undefined? [duplicat
  • Ember-data “Cannot call method 'hasOwnProperty' of undefined”
  • Javascript remove duplicate values from array of objects
  • javascript variables, What does var x = a = {} do?
  • Regex for nested values
  • Making header act like google chrome mobile app address bar
  • How gzip file gets stored in HDFS
  • Less Conflicting Session Manager for Zope 2
  • Does Mobilefirst provide a provision to access web services directly?
  • What and where is mdimport
  • Does it make sense to call System.gc() and Thread.sleep() when working on Bitmaps?
  • Jackson Parser: ignore deserializing for type mismatch
  • Time complexity of a program which involves multiple variables
  • one Local Olampyad Questions on Informatic in 2011
  • Initializer list vs. initialization method
  • Installing Hadoop, Java Exception about illegal characters at index 7?
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • Different response to non-authenticated users and AJAX calls
  • Get object from AWS S3 as a stream
  • Date difference with leap year
  • How can I use Kendo UI with Razor?
  • Rearranging Cells in UITableView Bug & Saving Changes
  • retrieve vertices with no linked edge in arangodb
  • Benchmarking RAM performance - UWP and C#
  • CSS Applying specific rule for a specific monitor resolution with only CSS is posible?
  • Angular 2 constructor injection vs direct access
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Change div Background jquery
  • How to get Windows thread pool to call class member function?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • apache spark aggregate function using min value
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal