NIM util 工具方法, 通过 NIM.util
来获取此工具的引用
Methods
-
<static> cutObjArray(base, arr1, arr2, options)
-
从数组里面去除某些项
Parameters:
Name Type Description base
Array 基数组
arr1
Array.<Object> 待去除的数组
arr2
Array.<Object> 待去除的数组
options
Object 配置参数
Properties
Name Type Argument Default Description keyPath
String <optional>
'id' keyPath
, 去重的字段, 默认为id
Returns:
去除后的数组
- Type
- Array
Example
var olds = [ { account: 'a' }, { account: 'b' }, { account: 'c' } ]; var invalids = [ { account: 'b' } ]; var options = { keyPath: 'account' }; var array = NIM.util.cutObjArray(olds, invalids, options); // array 为 // [ // { account: 'a' }, // { account: 'c' } // ]
-
<static> findObjInArray(array, options)
-
在数组里面找 keyPath 对应的属性值为 value 的元素
- 数组的每一项均为对象, 并且必须有由 keyPath 指定的属性
Parameters:
Name Type Description array
Array.<Object> 待查找的数组
options
Object 查找的条件
Properties
Name Type Argument Description keyPath
String <optional>
keyPath, 匹配的字段, 默认为 'id'
value
Anything <optional>
匹配的值
Returns:
找到的元素, 或者 null
- Type
- Object
Example
var array = [ {name: 'tom'}, {name: 'jack'}, {name: 'dan'} ]; var obj = NIM.util.findObjInArray(array, { keyPath: 'name', value: 'jack' }); // obj 为 {name: 'jack'}
-
<static> guid()
-
Returns:
guid/uuid
- Type
- String
-
<static> mergeObjArray(arr1, arr2 [, options])
-
合并数组
- 此方法接收不定量参数
- 最后一个参数如果是对象, 那么就是配置参数
- 除了配置参数之外, 所有其它的参数都必须是数组, 它们都会被合并
- 如果两个对象
keyPath
字段对应的属性值相同, 后面的对象会被合并到前面的对象
Parameters:
Name Type Argument Description arr1
Array.<Object> 待合并的数组
arr2
Array.<Object> 待合并的数组
options
Object <optional>
配置参数
Properties
Name Type Argument Default Description keyPath
String <optional>
'id' keyPath
, 去重的字段, 默认为id
notSort
Boolean <optional>
是否要排序, 默认
false
要排序, 传true
则不排序compare
function <optional>
决定排序的方法, 如果不提供, 那么使用
NIM.naturalSort
进行排序sortPath
String <optional>
sortPath
, 排序用的字段, 默认为keyPath
insensitive
Boolean <optional>
排序时是否不区分大小写, 默认区分
desc
Boolean <optional>
是否逆序, 默认正序
Returns:
合并并排序后的数组
- Type
- Array.<Object>
Example
var arr1 = [ { account: 'tom', name: 'T' } ]; var arr2 = [ { account: 'adam' }, { account: 'tom', name: 'T-new' } ]; var options = { keyPath: 'account' }; var resultArray = NIM.util.mergeObjArray(arr1, arr2, options); // resultArray为 // [ // {account: 'adam'}, // {account: 'tom', name: 'T-new'}, // ]
- 此方法接收不定量参数
-
<static> sortObjArray(array [, options])
-
返回排序后的数组
- 数组的每一项都为
Object
, 并且必须有由sortPath
指定的属性
Parameters:
Name Type Argument Description array
Array.<Object> 待排序的数组
options
Object <optional>
配置参数
Properties
Name Type Argument Description compare
function <optional>
决定排序的方法, 如果不提供, 那么使用
NIM.naturalSort
进行排序sortPath
String <optional>
sortPath
, 排序用的字段, 默认为id
insensitive
Boolean <optional>
排序时是否不区分大小写, 默认区分
desc
Boolean <optional>
是否逆序, 默认正序
Returns:
排序后的数组
- Type
- Array.<Object>
Example
var array = [ { account: 'b' }, { account: 'a' } ]; var options = { sortPath: 'account' }; NIM.util.sortObjArray(array, options); // array 为 //[ // { account: 'a' }, // { account: 'b' } //]
- 数组的每一项都为