How to use the isArray function from underscore

Find comprehensive JavaScript underscore.isArray code examples handpicked from public code repositorys.

underscore.isArray is a method used to determine if a given value is an array or not.

43
44
45
46
47
48
49
50
51

function maxword(field) {
  const validation = field.validate || [];
  const mw = _.findWhere(validation, { type: 'maxword' });
  if (mw) {
    return _.isArray(mw.arguments) ? mw.arguments[0] : mw.arguments;
  }
  return null;
}
fork icon14
star icon13
watch icon12

1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
r.getCrud( req ).read( r.getRequestId( req ), function( err, data, fullData ) {
    if( err )
        return next( err );

    /* no matching dataset, render "add" */
    if( _.isArray( data ) ) {
        req.messages.push( { type: "danger", title: req.locales.__("Error"), text: req.locales.__( "Unknown ID" ) } );
        renderAll( req, res, next );
    }
    /* dataset found, render "edit" */
fork icon3
star icon9
watch icon1

+ 5 other calls in file

How does underscore.isArray work?

underscore.isArray is a function in the Underscore.js library that determines if the given value is an array or not by checking its internal [[Class]] property. When called, underscore.isArray takes one argument, the value to check, and returns a boolean value of true if the value is an array and false otherwise. It works for arrays created in the same window or frame as the current context, as well as for arrays created in other windows or frames. The function first checks if the value is an object and then checks if its internal [[Class]] property is set to "Array".

2083
2084
2085
2086
2087
2088
2089
2090
2091
2092

// Return the maximum element or (element-based computation).
// Can't optimize arrays of integers longer than 65,535 elements.
// See [WebKit Bug 80797](https://bugs.webkit.org/show_bug.cgi?id=80797)
_.max = function(obj, iterator, context) {
  if (!iterator && _.isArray(obj) && obj[0] === +obj[0] && obj.length < 65535) {
    return Math.max.apply(Math, obj);
  }
  if (!iterator && _.isEmpty(obj)) return -Infinity;
  var result = {computed : -Infinity, value: -Infinity};
fork icon0
star icon2
watch icon0

+ 3 other calls in file

117
118
119
120
121
122
123
124
125
126
127
128
129


});


Alexa.out(function(message) {
  var server = this;
  var payload = _.isArray(message.payload) ? message.payload : [message.payload];
  return new Promise(function(resolve) {
    var response = message.originalMessage.getResponse();


    var alexaMessage = server.responseEnvelope();
fork icon0
star icon1
watch icon1

Ai Example

1
2
3
4
5
6
7
const _ = require("underscore");

const arr = [1, 2, 3];
const notArr = { a: 1, b: 2, c: 3 };

console.log(_.isArray(arr)); // true
console.log(_.isArray(notArr)); // false

In this example, the underscore library is used to check whether the arr variable is an array or not, which returns true. The notArr variable is an object, so _.isArray(notArr) returns false.

51
52
53
54
55
56
57
58
59
60
case 'integer':
  validator = validators.integer;
  break;
case 'params':
    validator = value => {
      return _.isArray(value) && value.every(param => {
        return _.isObject(param) && !_.isEmpty(param.platform) && !_.isEmpty(param.name) && param.value != null;
      });
    };
  break;
fork icon0
star icon1
watch icon1

+ 8 other calls in file

193
194
195
196
197
198
199
200
201
202
    throw new InvalidSchemaError('validation not yet implemented: ' + JSON.stringify(schema));
  }
};

var _validateRecord = function(schema, obj) {
  if (!_.isObject(obj) || _.isArray(obj)) {
    throw new ValidationError('Expected record Javascript type to be non-array object, got ' + JSON.stringify(obj));
  }

  var schemaFieldNames = _.pluck(schema.fields, 'name').sort();
fork icon0
star icon1
watch icon1

663
664
665
666
667
668
669
670
671
672
  return _.map(obj, function(value){ return value[key]; });
};

// Return the maximum element or (element-based computation).
_.max = function(obj, iterator, context) {
  if (!iterator && _.isArray(obj)) return Math.max.apply(Math, obj);
  if (!iterator && _.isEmpty(obj)) return -Infinity;
  var result = {computed : -Infinity};
  each(obj, function(value, index, list) {
    var computed = iterator ? iterator.call(context, value, index, list) : value;
fork icon0
star icon0
watch icon1

+ 20 other calls in file

91
92
93
94
95
96
97
98
99
100
  key: key + "-" + index,
  desc: ConcatDesc(copiedState.title, copiedState.description),
  required: required.indexOf(name) != -1
};

if (value.type === "object" || (_.isUndefined(value.type) && _.isArray(optionForm))) {
  item = Object.assign({}, item, { type: "object", children: optionForm });
  delete item.sub;
} else {
  item = Object.assign({}, item, optionForm);
fork icon0
star icon0
watch icon1

+ 7 other calls in file

209
210
211
212
213
214
215
216
217
218
//  按照columns的顺序排列数据
let tpl = ``;
dataSource.map(col => {
  let child = null;
  tpl += `<tr key=${col.key}>${tableCol(col, columns, level)}</tr>`;
  if (!_.isUndefined(col.children) && _.isArray(col.children)) {
    let index = level + 1;
    child = tableBody(col.children, columns, index);
  }
  tpl += child ? `${child}` : ``;
fork icon0
star icon0
watch icon1

93
94
95
96
97
98
99
100
101
102
  key: key + '-' + index,
  desc: ConcatDesc(copiedState.title, copiedState.description),
  required: required.indexOf(name) != -1
};

if (value.type === 'object' || (_.isUndefined(value.type) && _.isArray(optionForm))) {
  item = Object.assign({}, item, { type: 'object', children: optionForm });
  delete item.sub;
} else {
  item = Object.assign({}, item, optionForm);
fork icon0
star icon0
watch icon1

18
19
20
21
22
23
24
25
26
27
    this.current = 0;
    this.searchQuery = undefined;
}
render() {
    var filteredData = this.filteredData();
    var dictVal = _.isArray(filteredData) ? filteredData[this.current] : filteredData;

    $(this.el).html(cardDataViewTemplate({
        values: _.pairs(dictVal),
        searchQuery: this.searchQuery,
fork icon0
star icon0
watch icon2

3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
* @category Lang
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if `value` is an array, else `false`.
* @example
*
* _.isArray([1, 2, 3]);
* // => true
*
* _.isArray(document.body.children);
* // => false
fork icon0
star icon0
watch icon0

+ 17 other calls in file