npm i great-jsutils --save
import {JsUtilsApi, ArrayUtils, Base64, TimeUtils,StringUtils } from "great-jsutils/index";
//字符串移除,第一个参数是被操作的数组,第二个参数是需要移除的字符串
testArray(){
let arr=["a","b","c"];
ArrayUtils.remove(arr,"b");
console.log(arr.join(","));//a,c
}
//对象移除,第一个参数是被操作的数组,第二个参数是需要移除的对象,第三个对象的主键
testArray(){
let objArr=[{"id":"1","name":"赵云"},{"id":"2","name":"诸葛亮"}];
ArrayUtils.remove(objArr,{"id":"2"},"id");
console.log(JSON.stringify(objArr));//[{"id":"1","name":"赵云"}]
}
//数组中的对象key替换,例如源数据为[{"name":"张三"}]替换为[{"text":"张三"}]
testArray(){
let datas=[{"name":"张三","sex":"1"}];
let keys={"id":"key"};
let result=ArrayUtils.updateKey(data,keys);
console.log(result);//[{"text":"张三"}]
}
//数组中对象key替换,例如源数据为[{"name":"张三","age":80}]替换为[{"text":"张三","age":80,"ageGroup":"老年人"}];
//这里是根据某些值特征,增加新的属性
testArray(){
let datas=[{"name":"张三","sex":"1"}];
let keys={"name":"text"};
let result=ArrayUtils.updateKey(data,keys,function(){
if(data["age"]>75){
return {"ageGroup":"老年人"};
}
});
console.log(result);//[{"text":"张三","age":80,"ageGroup":"老年人"}];
}
function testArray(){
let data=[{"id":"id-A","type":"1","children":[
{"id":"id-A01","type":"2","children":[
{"id":"id-A01-01","type":"3"}
]},
{"id":"id-A03","type":"3"}
]}];
let keys={"id":"key"};
let result=ArrayUtils.updateKey(data,keys,function (data) {
if(typeof data == "object"){
if(!data["children"] || data["children"]==null ||data["children"]["lenght"]==0){
return {"isLeaf":true};
}else{
return {"childNum":data["children"]["length"]};
}
}
});
console.log(result);
}
testArray();
ArrayUtils.validArray(datas)
向指定节点,追加新的子节点
function testArray(){
let data=[{"id":"id-A","type":"1","children":[
{"id":"id-A01","type":"2","children":[
{"id":"id-A01-01","type":"3"}
]},
{"id":"id-A03","type":"3"}
]}];
let child={"id":"id-A03-01","pid":"id-A03"};
let newData=ArrayUtils.addByParentId(data,child,"pid","id","children");
console.log(result);
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。