1.实现字符串快速拼接
const str = ["qwdqd", "qweqwe", "qweqwe"];
const min = ["min-length", "fix-min", "this-min-pro", "x-min-pro"];
const strJoin = (str: string[]) => {
let newStr = str.join("");
return newStr;
};
console.log(strJoin(str));
2.过滤掉min开头或者min结尾的字符串
const filterJoin = (arr: string[]) => {
let newArr: string[] = [];
arr.forEach((item) => {
const newItem = item.split("-");
console.log(newItem, "数组");
if (newItem[0] !== "min" && newItem[newItem.length - 1] !== "min") {
newArr.push(item);
console.log(newArr);
}
});
let lastArr = newArr.join("").split("-").join("");
console.log(lastArr, "返回的数据");
return lastArr;
};
console.log(filterJoin(min));
5.过滤json对象。过滤json中的pos。
```
// var myJSON = '{ "name":"Bill Gates", "age":62, "city":"Seattle" }';
let jsonObj =
'{"and":[{"x":{"eq":"11","pos":"AA"}},{"y":{"eq":"22","pos":"BB"}}]}';
let obj = JSON.parse(jsonObj); //把json转成对象的格式
//把对象and数组进行forEach循环
obj.and.forEach((item: object) => {
console.log(item);
console.log(Object.keys(item));
console.log(Object.values(item)); //通过Object.values拿到对象的值 {eq:"11",pos:"AA"} 返回的是一个数组
let newObj = Object.values(item)[0]; //拿到数组中的对象 对对象进行for in 循环
for (var i in newObj) {
console.log(i);
if (i === "pos") {
//i为的键的值 如果等于 pos 则把 newObj 中相应的键值对删除掉。
delete newObj[i];
console.log(newObj);
}
}
});
console.log(obj);