webroo / dummy-json

Generates random dummy JSON data in Node.js
MIT License
380 stars 61 forks source link

How to pick a random value from random array #6

Closed devarajc closed 8 years ago

devarajc commented 8 years ago

Hi ,

I want to know about in dummy json how to get an random value from customize array. Let my array would be var color = ["blue" , "red", "green"]

Thanks

webroo commented 8 years ago

Here's a quick example of how to write a custom helper to pick a random item from an array:

var helpers = {
  color: function (options) {
    var colors = ['blue', 'red', 'green'];
    return colors[Math.floor(Math.random() * (colors.length))];
  }
};
var template = '{ "background": {{color}} }';
var result = dummyjson.parse(template, {helpers: helpers});

This is quite a common scenario so I plan to make it a little easier to implement in the next major release version.

devarajc commented 8 years ago

Its worked fine and Thank you so much

webroo commented 8 years ago

This has changed slightly since v1.0.0, for more information and an example see https://github.com/webroo/dummy-json#array-based-helpers