logo

/Code Snippet|v2.3.3

Module

Static Methods

forEach(obj, iteratee, context)forEach.jsline 47

Execute the provided callback once for each property of object(or element of array) which actually exist.
If the object is Array-like object(ex-arguments object), It needs to transform to Array.(see 'ex2' of example).
If the callback function returns false, the loop will be stopped.
Callback function(iteratee) is invoked with three arguments:
1) The value of the property(or The value of the element)
2) The name of the property(or The index of the element)
3) The object being traversed

PARAMETERS
NameTypeDescription

obj

Object

The object that will be traversed

iteratee

function

Callback function

context

[ Object ]

Context(this) of callback function

EXAMPLES
// ES6
import forEach from 'tui-code-snippet/collection/forEach'; 

// CommonJS
const forEach = require('tui-code-snippet/collection/forEach'); 

let sum = 0;

forEach([1,2,3], function(value){
  sum += value;
});
alert(sum); // 6

// In case of Array-like object
const array = Array.prototype.slice.call(arrayLike); // change to array
forEach(array, function(value){
  sum += value;
});

forEachArray(arr, iteratee, context)forEachArray.jsline 33

Execute the provided callback once for each element present
in the array(or Array-like object) in ascending order.
If the callback function returns false, the loop will be stopped.
Callback function(iteratee) is invoked with three arguments:
1) The value of the element
2) The index of the element
3) The array(or Array-like object) being traversed

PARAMETERS
NameTypeDescription

arr

ArrayArgumentsNodeList

The array(or Array-like object) that will be traversed

iteratee

function

Callback function

context

[ Object ]

Context(this) of callback function

EXAMPLES
// ES6
import forEachArray from 'tui-code-snippet/collection/forEachArray';

// CommonJS
const forEachArray = require('tui-code-snippet/collection/forEachArray'); 

let sum = 0;

forEachArray([1,2,3], function(value){
  sum += value;
});
alert(sum); // 6

forEachOwnProperties(obj, iteratee, context)forEachOwnProperties.jsline 32

Execute the provided callback once for each property of object which actually exist.
If the callback function returns false, the loop will be stopped.
Callback function(iteratee) is invoked with three arguments:
1) The value of the property
2) The name of the property
3) The object being traversed

PARAMETERS
NameTypeDescription

obj

Object

The object that will be traversed

iteratee

function

Callback function

context

[ Object ]

Context(this) of callback function

EXAMPLES
// ES6
import forEachOwnProperties from 'tui-code-snippet/collection/forEachOwnProperties';

// CommonJS
const forEachOwnProperties = require('tui-code-snippet/collection/forEachOwnProperties'); 

let sum = 0;

forEachOwnProperties({a:1,b:2,c:3}, function(value){
  sum += value;
});
alert(sum); // 6

pluck(arr, property)pluck.jsline 35

fetching a property

PARAMETERS
NameTypeDescription

arr

Array

target collection

property

StringNumber

property name

RETURNS:
{

Array

}
EXAMPLES
// ES6
import pluck from 'tui-code-snippet/collection/pluck'; 

// CommonJS
const pluck = require('tui-code-snippet/collection/pluck'); 

const objArr = [
  {'abc': 1, 'def': 2, 'ghi': 3},
  {'abc': 4, 'def': 5, 'ghi': 6},
  {'abc': 7, 'def': 8, 'ghi': 9}
];
const arr2d = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9]
];
pluck(objArr, 'abc'); // [1, 4, 7]
pluck(arr2d, 2); // [3, 6, 9]

toArray(arrayLike)toArray.jsline 34

Transform the Array-like object to Array.
In low IE (below 8), Array.prototype.slice.call is not perfect. So, try-catch statement is used.

PARAMETERS
NameTypeDescription

arrayLike

*

Array-like object

RETURNS:
{

Array

} - Array
EXAMPLES
// ES6
import toArray from 'tui-code-snippet/collection/toArray'; 

// CommonJS
const toArray = require('tui-code-snippet/collection/toArray'); 

const arrayLike = {
  0: 'one',
  1: 'two',
  2: 'three',
  3: 'four',
  length: 4
};
const result = toArray(arrayLike);

alert(result instanceof Array); // true
alert(result); // one,two,three,four
Resizable