Design & Download
Your JS Library
in Seconds!!!

Snippet: fill() - Filling Arrays & Objects

Written by Christopher West (cwest) on December 12, 2015.
Fill an array or an object with a specific value at all of its indices.
function fill(arrOrObj, value) {
  walk(arrOrObj, function(v, k) {
    arrOrObj[k] = value;
  }, 1);
  return arrOrObj;
}

fill(...) API Documentation

Description

Fill an array or an object with a specific value at all of its indices.

Parameters

  1. arrOrObj {Array|Object}:
    The array or object whose values will be replaced with the specified value.
  2. value {*}:
    The value to be placed at each index (or key).

Returns

Returns the modified version of arrOrObj with all of its values replaced with value.