bui.array Class
常用小方法
Item Index
Methods
compare
-
array
-
value
-
[key]
比对数组是否已经存在,支持普通数组及JSON数组, 1.5.2 写法统一为数组在前, 1.5.2以前的版本是数组在后.
Parameters:
-
array
Array[数组]
-
value
String[要比对的值值]
-
[key]
String optionalobject的键名
Returns:
[返回true|false]
Example:
//例子1: 普通数组:
var arr = ["hello","bui","hi","bui"];
var isExist = bui.array.compare( arr, "bui" );
// console.log(isExist) //结果: true
// 例子2: JSON数组:
var arr = [{ "id":1,value:"hello"},{ "id":2,value:"bui"}];
var isExist = bui.array.compare( arr, "bui", "value" );
// console.log(isExist) //结果: true
// 例子3 1.7.0 新增
var arr = [{ "id":1,value:"hello"},{ "id":2,value:"bui"}];
var isExist = bui.array.compare( arr, { "id":2,value:"bui"} );
copy
-
array
-
[from]
-
[length]
复制数组某一部分, 建议使用数组的 slice 方法处理
Parameters:
-
array
Array[数组]
-
[from]
Number optional从第几个复制,默认为0,复制整个数组则不用传
-
[length]
Number optional复制多少个,不填则是到最后一个
Returns:
[返回一个新的数组]
Example:
var arr = ["hello","bui","hi","easybui"];
var newArr = bui.array.copy( arr, 1 );
var newArr2 = bui.array.copy( arr, 1,2 );
// console.log(newArr) //结果: ["bui","hi","easybui"]
// console.log(newArr2) //结果: ["bui","hi"]
delete
-
array
-
value
-
[key]
删除后返回索引, 会改变原有数组
Parameters:
-
array
Array[数组]
-
value
String | Array[要删除的数据,值]
-
[key]
String | Number optionalobject的键名,非object可以不用传
Returns:
[返回删除前的所在位置]
Example:
//例子1: 删除值:
var arr = ["hello","bui","hi"];
bui.array.delete(arr , "bui" );
// 例子2: 删除值在哪个字段:
var arr = [{ "id":1,value:"hello"},{ "id":2,value:"bui"}];
bui.array.delete( arr, "bui", "value" );
// 例子3: 1.7.0 新增
var arr = [{ "id":1,value:"hello"},{ "id":2,value:"bui"}];
bui.array.delete( arr, { "id":2,value:"bui"} );
deleteIndex
-
array
-
value
删除第几个索引
Parameters:
-
array
Array[数组]
-
value
Number | Array[要删除的索引]
Returns:
[返回删除的内容]
Example:
//例子1: 删除值:
var arr = ["hello","bui","hi"];
var newArr = bui.array.deleteIndex(arr , 0 );
// newArr 为删除的内容 ["hello"]
// 例子2: 删除值在哪个字段:
var arr = [{ "id":1,value:"hello"},{ "id":2,value:"bui"}];
var newArr = bui.array.deleteIndex( arr, 1 );
// newArr 为删除的内容 [{ "id":2,value:"bui"}]
empty
-
array
清空数组
Parameters:
-
array
Array[数组]
Returns:
[空数组]
Example:
var arr = ["hello","bui","hi","easybui"];
bui.array.empty( arr );
结果 arr: []
excess
-
array
-
value
-
[key]
去除数组的某个多余数据,支持普通数组及JSON数组, 1.5.2 写法统一为数组在前, 1.5.2以前的版本是数组在后.
Parameters:
-
array
Array[数组]
-
value
String[要匹配的值]
-
[key]
String optionalobject的键名
Returns:
[返回一个没有重复数据的数组]
Example:
//普通数组:
var arr = ["hello","bui","hi","bui"];
var newArr = bui.array.excess( arr,"bui" );
// console.log(newArr) //结果: ["hello","bui","hi"]
//JSON数组:
var arr = [{ "id":1,value:"hello"},{ "id":2,value:"bui"},{ "id":3,value:"bui"}];
var newArr = bui.array.excess( arr,"bui","value" );
// console.log(newArr) //结果: [{ "id":1,value:"hello"},{ "id":2,value:"bui"}]
filter
-
array
-
value
-
[key]
模糊筛选数组,支持普通数组及JSON数组, 1.5.2 写法统一为数组在前, 1.5.2以前的版本是数组在后.
Parameters:
-
array
Array[数组]
-
value
String[要查找的值]
-
[key]
String optionalobject的键名
Returns:
[返回筛选的元素]
Example:
//普通数组:
var arr = ["hello","bui","hi","easybui"];
var newArr = bui.array.filter(arr,"bui");
// console.log(newArr) //结果: ["bui","easybui"]
//JSON数组:
var arr = [{ "id":1,value:"hello"},{ "id":2,value:"bui"},{ "id":3,value:"easybui"}];
var newArr = bui.array.filter( arr,"bui","value" );
// console.log(newArr) //结果: [{ "id":2,value:"bui"},{ "id":3,value:"easybui"}]
get
-
array
-
value
-
[key]
返回存在的值,支持普通数组及JSON数组,get只取第一个完全匹配的值
Parameters:
-
array
Array[数组]
-
value
String[要查找的值]
-
[key]
String optional如果是object,需要传该值在哪个键名里
Returns:
[返回筛选的第一个符合的元素]
Example:
//普通数组:
var arr = ["hello","bui","hi","easybui"];
var item = bui.array.get( arr,"bui" );
//JSON数组:
var arr = [{ "id":1,value:"hello"},{ "id":2,value:"bui"},{ "id":3,value:"easybui"}];
var item = bui.array.get( arr, "bui", "value" );
// console.log(newArr) //结果: { "id":2,value:"bui"}
getAll
-
array
-
value
-
[key]
返回存在的所有值,支持普通数组及JSON数组, filter返回多个匹配值,getAll获取完全匹配的数据
Parameters:
-
array
Array[数组]
-
value
String[要查找的值]
-
[key]
String optional如果是object,需要传该值在哪个键名里
Returns:
[返回数组]
Example:
//普通数组:
var arr = ["hello","bui","hi","easybui"];
var item = bui.array.getAll( arr,"bui" );
// console.log(item) //结果: ["bui"]
//JSON数组:
var arr = [{ "id":1,value:"hello"},{ "id":2,value:"bui"},{ "id":3,value:"bui"}];
var item = bui.array.getAll( arr, "bui", "value" );
// console.log(newArr) //结果: [{ "id":2,value:"bui"},{ "id":3,value:"bui"}]
index
-
array
-
value
-
[key]
返回存在的第1个位置,支持普通数组及JSON数组, 1.5.2 写法统一为数组在前, 1.5.2以前的版本是数组在后.
Parameters:
-
array
Array数组
-
value
String要检测的值
-
[key]
String optionalobject的键名
Returns:
返回的位置如果大于0,则存在
Example:
1.5.2
//普通数组:
var arr = ["hello","bui","hi","bui"];
var index = bui.array.index( arr, "bui" );
// console.log(index) //结果: 1
//JSON数组:
var arr = [{ "id":1,value:"hello"},{ "id":2,value:"bui"}];
var index = bui.array.index( arr, "bui", "value" );
// console.log(index) //结果: 1
indexs
-
array
-
value
-
[key]
返回数组的所有索引,支持普通数组及JSON数组, 1.5.2 写法统一为数组在前,值在后, 1.5.2以前的版本是值在前,数组在后.
Parameters:
-
array
Array[数组]
-
value
String[要匹配的值]
-
[key]
String optionalobject的键名
Returns:
[返回存在的索引数组]
Example:
//普通数组:
var arr = ["hello","bui","hi","bui"];
var index = bui.array.indexs( arr,"bui" );
// console.log(index) //结果: [1,3]
//JSON数组:
var arr = [{ "id":1,value:"hello"},{ "id":2,value:"bui"}];
var index = bui.array.indexs( arr,"bui", "value" );
// console.log(index) //结果: [1]
merge
-
array
-
arrayA
合并2个数组, A数组+B数组+C数组 并会触发 A数组的更新
Parameters:
-
array
Array[数组]
-
arrayA
Array[支持多个数组]
Returns:
[空数组]
Example:
var arr = ["hello","hi","easybui"];
bui.array.merge( arr, ["new","bui"]);
// 结果 arr: ["hello","hi","easybui","new","bui","easy"]
// 支持对象合并相同字段,不同字段的则会新增
var arr = [{id:"1",name:"test1"},{id:"2",name:"test2"}];
bui.array.merge(arr,[{id:"1",sex:"男"},{id:"3",name:"test3"}],"id")
remove
-
array
-
value
-
[key]
会修改原数组, 删除数组的某个值或者对象,支持普通数组及JSON数组,并且1.5.1支持删除多个数据. 1.5.2 写法统一为数组在前, 1.5.2以前的版本是数组在后.
Parameters:
-
array
Array[数组]
-
value
String[要删除的值]
-
[key]
String optional删除的值在哪个字段
Returns:
[返回删除后的数组]
Example:
//普通数组:
var arr = ["hello","bui","hi","bui"];
bui.array.remove( arr, "bui" );
// console.log(newArr) //结果: ["hello","hi"]
//JSON数组:
var arr = [{ "id":1,value:"hello"},{ "id":2,value:"bui"}];
bui.array.remove( arr, "bui", "value" );
// console.log(newArr) //结果: [{ "id":1,value:"hello"}]
//1.7.0 新增:
var arr = [{ "id":1,value:"hello"},{ "id":2,value:"bui"}];
bui.array.remove( arr, { "id":2,value:"bui"} );
// console.log(newArr) //结果: [{ "id":1,value:"hello"}]
replace
-
array
替换数组
Parameters:
-
array
Array[数组]
Returns:
[空数组]
Example:
var arr = ["hello","hi","easybui"];
bui.array.replace( arr, ["new","bui"]);
// 结果 arr: ["new","bui"]
set
-
array
-
key|value
-
[value]
-
[keyname]
修改数组或数组对象里面的某个值,结合store触发数据变更, 有3个参数时,第2个是索引值或者数组某个值, 如果是对象,需要第4个参数
Parameters:
-
array
Array[数组]
-
key|value
Number | String | Object | Array[ 数字索引值,或者是值, 如果是数字,设置索引, 如果是其它值则比对值 ]
-
[value]
Number | String | Object optional修改的值
-
[keyname]
String optional如果比对的是对象,则需要这个键值
Returns:
[空数组]
Example:
例子1: 修改第几个
var arr = ["hello","hi","easybui"];
bui.array.set( arr, 1, "new hi");
// ["hello","new hi","easybui"]
1.5.3 版本以后可以这样
arr.$set(1, "new hi");
// arr 结果: ["hello","new hi","easybui"]
例子2: 修改值等于 hi 为新值 new hi
var arr = ["hello","hi","easybui"];
bui.array.set( arr, "hi", "new hi");
// ["hello","new hi","easybui"]
1.5.3 版本以后可以这样
arr.$set("hi", "new hi");
// arr 结果: ["hello","new hi","easybui"]
例子3: 修改对象值
var arr = [{name:"hello"},{name:"hi"},{name:"easybui"}];
bui.array.set( arr, 1, {name:"new hi"} );
// [{name:"hello"},{name:"new hi"},{name:"easybui"}]
1.5.3 版本以后可以这样
arr.$set(1, {name:"new hi"});
// arr 结果: [{name:"hello"},{name:"new hi"},{name:"easybui"}]
例子4: 修改对象某个字段值, 需要传多一个唯一值的字段名
var arr = [{name:"hello"},{name:"hi"},{name:"easybui"}];
bui.array.set( arr, "hi", {name:"new hi"}, "name" );
// [{name:"hello"},{name:"new hi"},{name:"easybui"}]
1.5.3 版本以后可以这样
// 单独修改某个值
arr.$set("hello", "hi bui", "name");
// 修改整个对象,不同的key值则会一起合并过去
arr.$set("hi", {name:"new hi"}, "name");
// arr 结果: [{name:"hello"},{name:"new hi"},{name:"easybui"}]
setAll
-
array
-
key|value
-
[value]
-
[keyname]
修改所有相同的值
Parameters:
-
array
Array[数组]
-
key|value
Number | String | Object | Array[ 数字索引值,或者是值, 如果是数字,设置索引, 如果是其它值则比对值 ]
-
[value]
Number | String | Object optional修改的值
-
[keyname]
String optional如果比对的是对象,则需要这个键值
Returns:
[空数组]
Example:
例子1: 修改值等于 hi 为新值 new hi
var arr = ["hello","hi","easybui","hi","hi"];
bui.array.setAll( arr, "hi", "new hi");
// ["hello","new hi","easybui","new hi","new hi"]
例子2: 修改对象某个字段值, 需要传多一个唯一值的字段名
var arr = [{name:"hello"},{name:"hi"},{name:"hi"},{name:"easybui"}];
bui.array.setAll( arr, "hi", "new hi", "name" );
// [{name:"hello"},{name:"new hi"},{name:"new hi"},{name:"easybui"}]
例子3: 替换对象
var arr = [{name:"hello"},{name:"hi"},{name:"hi"},{name:"easybui"}];
bui.array.setAll( arr, "hi", {name:"new hi","id":123}, "name" );
// [{name:"hello"},{name: "new hi", id: 123},{name: "new hi", id: 123},{name:"easybui"}]
uniq
-
array
-
[key]
数组去重,支持普通数组及JSON数组
Parameters:
-
array
Array[数组]
-
[key]
String optional数组里,如果是object,进行去重的值在哪个键名
Returns:
[返回一个没有重复数据的数组]
Example:
//普通数组:
var arr = ["hello","bui","hi","bui"];
var newArr = bui.array.uniq( arr );
// console.log(newArr) //结果: ["hello","bui","hi"]
//JSON数组:
var arr = [{ "id":1,value:"hello"},{ "id":2,value:"bui"},{ "id":3,value:"bui"}];
var newArr = bui.array.uniq( arr, "value" );
// console.log(newArr) //结果: [{ "id":1,value:"hello"},{ "id":2,value:"bui"}]