BUI

其它版本:

API for BUI 1.5.4

Show:

bui.array Class

常用小方法

Methods

compare

(
  • array
  • value
  • [key]
)
Boolean

比对数组是否已经存在,支持普通数组及JSON数组, 1.5.2 写法统一为数组在前, 1.5.2以前的版本是数组在后.

Parameters:

  • array Array

    [数组]

  • value String

    [要比对的值值]

  • [key] String optional

    [object的键名]

Returns:

Boolean:

[返回true|false]

Example:

   //普通数组:
   var arr = ["hello","bui","hi","bui"];
   var isExist = bui.array.compare( arr, "bui" );

   // console.log(isExist) //结果: true

   //JSON数组:
   var arr = [{ "id":1,value:"hello"},{ "id":2,value:"bui"}];
   var isExist = bui.array.compare( arr, "bui", "value" );
   // console.log(isExist) //结果: true

copy

(
  • array
  • [from]
  • [length]
)
Array

1.5.3 后废弃, 复制数组某一部分, 建议使用数组的 slice 方法处理

Parameters:

  • array Array

    [数组]

  • [from] Number optional

    [从第几个复制,默认为0,复制整个数组则不用传]

  • [length] Number optional

    [复制多少个,不填则是到最后一个]

Returns:

Array:

[返回一个新的数组]

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]
)
Array

跟remove的区别在于这个接口返回删除前的所在索引, 便于两个相同数据进行比对

Parameters:

  • array Array

    [数组]

  • value String | Number | Array

    [要删除的数据,值,或者索引]

  • [key] String | Number optional

    [object的键名,非object可以不用传]

Returns:

Array:

[返回删除前的所在位置]

Example:

   //例子1: 删除值或索引:
   var arr = ["hello","bui","hi","bui"];
   bui.array.delete(arr , "bui" );

    1.5.3 版本以后可以这样
   arr.$delete("hi");
   // arr 结果: ["hello","hi"]


   // 例子2: 删除值在哪个字段:
   var arr = [{ "id":1,value:"hello"},{ "id":2,value:"bui"}];
   bui.array.delete( arr, "bui", "value" );

    1.5.3 版本以后可以这样
   arr.$delete("bui", "value");
   // arr 结果: [{ "id":1,value:"hello"}]

empty

(
  • array
)
Array

清空数组

Parameters:

  • array Array

    [数组]

Returns:

Array:

[空数组]

Example:

   var arr = ["hello","bui","hi","easybui"];
   bui.array.empty( arr );

   结果 arr: []

excess

(
  • array
  • value
  • [key]
)
Array

1.5.1以后废弃,使用 bui.array.uniq(arr) 替代. 去除数组的某个多余数据,支持普通数组及JSON数组, 1.5.2 写法统一为数组在前, 1.5.2以前的版本是数组在后.

Parameters:

  • array Array

    [数组]

  • value String

    [要匹配的值]

  • [key] String optional

    [object的键名]

Returns:

Array:

[返回一个没有重复数据的数组]

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]
)
Array

模糊筛选数组,支持普通数组及JSON数组, 1.5.2 写法统一为数组在前, 1.5.2以前的版本是数组在后.

Parameters:

  • array Array

    [数组]

  • value String

    [要查找的值]

  • [key] String optional

    [object的键名]

Returns:

Array:

[返回筛选的元素]

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]
)
Array

返回存在的值,支持普通数组及JSON数组, filter返回多个匹配值,get只取第一个完全匹配的值

Parameters:

  • array Array

    [数组]

  • value String

    [要查找的值]

  • [key] String optional

    [如果是object,需要传该值在哪个键名里]

Returns:

Array:

[返回筛选的第一个符合的元素]

Example:

    //普通数组:
    var arr = ["hello","bui","hi","easybui"];
    var item = bui.array.get( arr,"bui" );

    1.5.3 版本以后可以这样
   var item = arr.$get("bui" );
   // 结果: bui 如果没有则是 undefined 

    //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"}

   1.5.3 版本以后可以这样
   var item = arr.$get("bui", "value");
   // 结果: { "id":2,value:"bui"} 如果没有则是 undefined 

getAll

(
  • array
  • value
  • [key]
)
Array

返回存在的所有值,支持普通数组及JSON数组, filter返回多个匹配值,get只取第一个完全匹配的值

Parameters:

  • array Array

    [数组]

  • value String | Array

    [要查找的值,1.5.2 20190402 以后版本支持数组,匹配多个]

  • [key] String optional

    [如果是object,需要传该值在哪个键名里]

Returns:

Array:

[返回数组]

Example:

    //普通数组:
    var arr = ["hello","bui","hi","easybui"];
    var item = bui.array.getAll( arr,"bui" );

    // console.log(item) //结果: ["bui","easybui"]

    //JSON数组:
    var arr = [{ "id":1,value:"hello"},{ "id":2,value:"bui"},{ "id":3,value:"easybui"}];
    var item = bui.array.getAll( arr, "bui", "value" );
    // console.log(newArr) //结果: [{ "id":2,value:"bui"},{ "id":3,value:"easybui"}]

index

(
  • array
  • value
  • [key]
)
Number

返回存在的第1个位置,支持普通数组及JSON数组, 1.5.2 写法统一为数组在前, 1.5.2以前的版本是数组在后.

Parameters:

  • array Array

    [数组]

  • value String

    [要检测的值]

  • [key] String optional

    [object的键名]

Returns:

Number:

[返回的位置如果大于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]
)
Array

返回数组的所有索引,支持普通数组及JSON数组, 1.5.2 写法统一为数组在前,值在后, 1.5.2以前的版本是值在前,数组在后.

Parameters:

  • array Array

    [数组]

  • value String

    [要匹配的值]

  • [key] String optional

    [object的键名]

Returns:

Array:

[返回存在的索引数组]

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
)
Array

合并2个数组, A数组+B数组+C数组 并会触发 A数组的更新

Parameters:

  • array Array

    [数组]

  • arrayA Array

    [支持多个数组]

Returns:

Array:

[空数组]

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]
)
Array

删除数组的某个值或者对象,支持普通数组及JSON数组,并且1.5.1支持删除多个数据. 1.5.2 写法统一为数组在前, 1.5.2以前的版本是数组在后.

Parameters:

  • array Array

    [数组]

  • value String

    [要删除的值]

  • [key] String | Number optional

    [object的键名,或者数组的索引]

Returns:

Array:

[返回删除后的数组]

Example:

   //普通数组:
   var arr = ["hello","bui","hi","bui"];
   var newArr = bui.array.remove( arr, "bui" );

   // console.log(newArr) //结果: ["hello","hi"]

   //JSON数组:
   var arr = [{ "id":1,value:"hello"},{ "id":2,value:"bui"}];
   var newArr = bui.array.remove( arr, "bui", "value" );
   // console.log(newArr) //结果: [{ "id":1,value:"hello"}]

replace

(
  • array
)
Array

替换数组

Parameters:

  • array Array

    [数组]

Returns:

Array:

[空数组]

Example:

   var arr = ["hello","hi","easybui"];
   bui.array.replace( arr, ["new","bui"]);

   // 结果 arr: ["new","bui"]

set

(
  • array
  • key|value
  • [value]
  • [keyname]
)
Array

修改数组或数组对象里面的某个值,结合store触发数据变更, 有3个参数时,第2个是索引值或者数组某个值, 如果是对象,需要第4个参数

Parameters:

  • array Array

    [数组]

  • key|value Number | String | Object | Array

    [ 数字索引值,或者是值, 如果是数字,设置索引, 如果是其它值则比对值 ]

  • [value] Number | String | Object optional

    [ 修改的值 ]

  • [keyname] String optional

    [如果比对的是对象,则需要这个键值]

Returns:

Array:

[空数组]

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"}]

uniq

(
  • array
  • [key]
)
Array

数组去重,支持普通数组及JSON数组

Parameters:

  • array Array

    [数组]

  • [key] String optional

    [数组里,如果是object,进行去重的值在哪个键名]

Returns:

Array:

[返回一个没有重复数据的数组]

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"}]