您好,登錄后才能下訂單哦!
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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。