BUI

其它版本:

API for BUI 1.5.1

Show:

bui.array Class

常用小方法

Methods

compare

(
  • value
  • arr
  • [key]
)
Boolean

比对数组是否已经存在,支持普通数组及JSON数组

Parameters:

  • value String

    [要比对的值值]

  • arr Array

    [数组]

  • [key] String optional

    [object的键名]

Returns:

Boolean:

[返回true|false]

Example:

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

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

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

copy

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

复制数组某一部分

Parameters:

  • arr 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

(
  • arr
  • name
  • [key]
)
Array

跟remove的区别在于这个接口跟set一致,先数组

Parameters:

  • arr Array

    [数组]

  • name String | Number | Array

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

  • [key] String | Number optional

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

Returns:

Array:

[返回删除后的数组]

Example:

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

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

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

empty

(
  • arr
)
Array

清空数组

Parameters:

  • arr Array

    [数组]

Returns:

Array:

[空数组]

Example:

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

excess

(
  • value
  • arr
  • [key]
)
Array

1.5.1废弃,使用 bui.array.uniq(arr) 替代. 去除数组的某个多余数据,支持普通数组及JSON数组

Parameters:

  • value String

    [要匹配的值]

  • arr Array

    [数组]

  • [key] String optional

    [object的键名]

Returns:

Array:

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

Example:

    //普通数组:
    var arr = ["hello","bui","hi","bui"];
    var newArr = bui.array.excess( "bui", 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.excess( "bui", arr, "value" );
    // console.log(newArr) //结果: [{ "id":1,value:"hello"},{ "id":2,value:"bui"}] 

filter

(
  • value
  • arr
  • [key]
)
Array

筛选数组,支持普通数组及JSON数组

Parameters:

  • value String

    [要查找的值]

  • arr Array

    [数组]

  • [key] String optional

    [object的键名]

Returns:

Array:

[返回筛选的元素]

Example:

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

    // 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( "bui", arr, "value" );
    // console.log(newArr) //结果: [{ "id":2,value:"bui"},{ "id":3,value:"easybui"}] 

index

(
  • value
  • arr
  • [key]
)
Number

返回存在的第1个位置,支持普通数组及JSON数组

Parameters:

  • value String

    [要检测的值]

  • arr Array

    [数组]

  • [key] String optional

    [object的键名]

Returns:

Number:

[返回的位置如果大于0,则存在]

Example:

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

   // console.log(index) //结果: 1

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

indexs

(
  • value
  • arr
  • [key]
)
Array

返回数组的所有索引,支持普通数组及JSON数组

Parameters:

  • value String

    [要匹配的值]

  • arr Array

    [数组]

  • [key] String optional

    [object的键名]

Returns:

Array:

[返回存在的索引数组]

Example:

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

   // console.log(index) //结果: [1,3]

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

merge

(
  • arr
  • arrA
)
Array

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

Parameters:

  • arr Array

    [数组]

  • arrA Array

    [支持多个数组]

Returns:

Array:

[空数组]

Example:

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

remove

(
  • value
  • arr
  • [key]
)
Array

删除数组的某个值或者对象,支持普通数组及JSON数组,并且1.5.1支持删除多个数据

Parameters:

  • value String | Array

    [要删除的值或数组]

  • arr Array

    [数组]

  • [key] String | Number optional

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

Returns:

Array:

[返回删除后的数组]

Example:

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

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

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

replace

(
  • arr
)
Array

替换数组

Parameters:

  • arr Array

    [数组]

Returns:

Array:

[空数组]

Example:

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

set

(
  • arr
  • key
)
Array

修改数组及数组对象,结合store触发数据变更

Parameters:

  • arr Array

    [数组]

  • key Number | String

    []

Returns:

Array:

[空数组]

Example:

   例子1: 修改索引值
   var arr = ["hello","hi","easybui"];
   bui.array.set( arr, 1, "new hi");
   // ["hello","new hi","easybui"]

   例子2: 修改值
   var arr = ["hello","hi","easybui"];
   bui.array.set( arr, "hi", "new hi");
   // ["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"}]

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

uniq

(
  • arr
  • [key]
)
Array

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

Parameters:

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