重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
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);
}
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。