重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
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);
}