---
layout: page
title: "JavaScript extract function"
comments: true
sharing: true
footer: true
alias:
- /functions/view/extract:397
- /functions/view/extract
- /functions/view/397
- /functions/extract:397
- /functions/397
---
<!-- Generated by Rakefile:build -->
A JavaScript equivalent of PHP's extract

{% codeblock array/extract.js lang:js https://raw.github.com/kvz/phpjs/master/functions/array/extract.js raw on github %}
function extract (arr, type, prefix) {
  // From: http://phpjs.org/functions
  // +   original by: Brett Zamir (http://brett-zamir.me)
  // %        note 1: Only works by extracting into global context (whether called in the global scope or
  // %        note 1: within a function); also, the EXTR_REFS flag I believe can't be made to work
  // *     example 1: size = 'large';
  // *     example 1: var_array = {'color' : 'blue', 'size' : 'medium', 'shape' : 'sphere'};
  // *     example 1: extract(var_array, 'EXTR_PREFIX_SAME', 'wddx');
  // *     example 1: color+'-'+size+'-'+shape+'-'+wddx_size;
  // *     returns 1: 'blue-large-sphere-medium'
  if (Object.prototype.toString.call(arr) === '[object Array]' &&
    (type !== 'EXTR_PREFIX_ALL' && type !== 'EXTR_PREFIX_INVALID')) {
    return 0;
  }
  var targetObj = this.window;
  if (this.php_js && this.php_js.ini && this.php_js.ini['phpjs.extractTargetObj'] && this.php_js.ini['phpjs.extractTargetObj'].local_value) { // Allow designated object to be used instead of window
    targetObj = this.php_js.ini['phpjs.extractTargetObj'].local_value;
  }
  var chng = 0;

  for (var i in arr) {
    var validIdent = /^[_a-zA-Z$][\w|$]*$/; // TODO: Refine regexp to allow JS 1.5+ Unicode identifiers
    var prefixed = prefix + '_' + i;
    try {
      switch (type) {
      case 'EXTR_PREFIX_SAME' || 2:
        if (targetObj[i] !== undefined) {
          if (prefixed.match(validIdent) !== null) {
            targetObj[prefixed] = arr[i];
            ++chng;
          }
        } else {
          targetObj[i] = arr[i];
          ++chng;
        }
        break;
      case 'EXTR_SKIP' || 1:
        if (targetObj[i] === undefined) {
          targetObj[i] = arr[i];
          ++chng;
        }
        break;
      case 'EXTR_PREFIX_ALL' || 3:
        if (prefixed.match(validIdent) !== null) {
          targetObj[prefixed] = arr[i];
          ++chng;
        }
        break;
      case 'EXTR_PREFIX_INVALID' || 4:
        if (i.match(validIdent) !== null) {
          if (prefixed.match(validIdent) !== null) {
            targetObj[prefixed] = arr[i];
            ++chng;
          }
        } else {
          targetObj[i] = arr[i];
          ++chng;
        }
        break;
      case 'EXTR_IF_EXISTS' || 6:
        if (targetObj[i] !== undefined) {
          targetObj[i] = arr[i];
          ++chng;
        }
        break;
      case 'EXTR_PREFIX_IF_EXISTS' || 5:
        if (targetObj[i] !== undefined && prefixed.match(validIdent) !== null) {
          targetObj[prefixed] = arr[i];
          ++chng;
        }
        break;
      case 'EXTR_REFS' || 256:
        throw 'The EXTR_REFS type will not work in JavaScript';
      case 'EXTR_OVERWRITE' || 0:
        // Fall-through
      default:
        targetObj[i] = arr[i];
        ++chng;
        break;
      }
    } catch (e) { // Just won't increment for problem assignments
    }
  }
  return chng;
}
{% endcodeblock %}

 - [Raw function on GitHub](https://github.com/kvz/phpjs/blob/master/functions/array/extract.js)

Please note that php.js uses JavaScript objects as substitutes for PHP arrays, they are 
the closest match to this hashtable-like data structure. 

Please also note that php.js offers community built functions and goes by the 
[McDonald's Theory](https://medium.com/what-i-learned-building/9216e1c9da7d). We'll put online 
functions that are far from perfect, in the hopes to spark better contributions. 
Do you have one? Then please just: 

 - [Edit on GitHub](https://github.com/kvz/phpjs/edit/master/functions/array/extract.js)

### Example 1
This code
{% codeblock lang:js example %}
size = 'large';
var_array = {'color' : 'blue', 'size' : 'medium', 'shape' : 'sphere'};
extract(var_array, 'EXTR_PREFIX_SAME', 'wddx');
color+'-'+size+'-'+shape+'-'+wddx_size;
{% endcodeblock %}

Should return
{% codeblock lang:js returns %}
'blue-large-sphere-medium'
{% endcodeblock %}


### Other PHP functions in the array extension
{% render_partial _includes/custom/array.html %}
