2020-07_开发经验集

1、问题描述:生成报告到指定文件夹,没有则创建目录文件夹。
// 文件目录
String path = "D:/report";
File f = new File(path);
if (!f.exists()) {
    f.mkdirs();
}
2、问题描述:maven打包跳过单元测试。
<build>
    <plugins>                    
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <skipTests>true</skipTests>
            </configuration>
        </plugin>
    </plugins>
</build>
3、问题描述:centos 磁盘空间不足,如何查找目录占用空间情况?
du -h --max-depth=1

在这里插入图片描述

4、问题描述:React Hooks 开发时,启动总是提示hooks 语法错误?

原因:react声明组件时,第一个字母必须大写。

5、问题描述:React 开发菜单目录树结构时,数据结构如何定义?
/**
 * 生成树结构数据
 * @param {[]} data 数据源
 * @param {String} idPropName  字段属性名
 * @param {String} parentIdPropName 父字段属性名   
 */
const genTreeData = (data, idPropName, parentIdPropName='parentId') => {
	if (!Array.isArray(data)) {
		return [];
	}
	// 根据 parentIdPropName='parentId' 查找列表信息
	const findDataByPid = pid => {
		return data.filter(item => item[parentIdPropName] === pid);
	}
	
	// 递归拼接children
	const appendChildren = data => {
		for (let item of data) {
			const children = findDataByPid(item[idPropName]);
			if (children && children.length > 0) {
				item.children = children;
				appendChildren(children);
				item.lastLevel = false;
			} else if ('children' in item) {
				delete item.children;
				item.lastLevel = false;
			} else {
				// 最后一层属性
				item.lastLevel = true;
			}
		}
	} 
	
	const rootData = findDataByPid(0);
	appendChildren(rootData);
	return rootData;
}
6、问题描述:JS 的filter、map、forEach用法?
  • filter()是通过删选oldArray,来生产newArray的方法。
array.filter(function(value, index, arr),thisValue)

value: 必须,代表当前元素,其他四个参数都是可选,index代表当前索引值,arr代表当前的数组,      thisValue代表传递给函数的值,一般用this值,如果这个参数为空,undefined会传递给this值

返回值:返回数组,包含了符合条件的所有元素,如果没有符合条件的则返回空数组。

用法:

var arr = [1,2,3,4,5,6,7];
var ar = arr.filter(function(elem){
     return elem>5;
 });
 console.log(ar);//[6,7]
  • forEach()用于遍历数组中的每个元素,并将元素传递给回调函数。它没有返回值,直接修改原数组中的数据。
array.forEach(function(value, index, arr),thisValue)
value:必须,代表当前元素,其他四个参数都是可选,index代表当前索引值,arr代表当前的数组,thisValue代表传递给函数的值,一般用this值,如果这个参数为空,undefined会传递给this值。

用法:

let arr = [
  {   name: '1',
      id: '1'
  },{ name: '2',
      id: '2'
  },{   name: '3',
      id: '3'
  }
]
arr.forEach(item=>{
  if(item.name==='2'){
    item.name = 'zding'
  }
})

console.log(arr)
 [
  {   name: '1',
      id: '1'
  },{ name: 'zding',
      id: '2'
  },{   name: '3',
      id: '3'
  }
]

当数组中为单类型数据时:string、int等类型,这种方式的修改就不起作用了
let arr = [1,3,5,7,9]
arr.forEach(function(item){
        item = 30
 })
console.log(arr)  //输出  [1, 3, 5, 7, 9]   
期望输输出 [30, 30, 30, 30, 30],但实际上输出为 [1, 3, 5, 7, 9],修改没有起作用。
这时可以使用for循环,或者map()方法。
  • map()返回一个新数组,数组中的元素为原始数组元素调用函数处理后的值,map()方法按照原始数组元素顺序依次处理元素。
array.map(function(value, index, arr),thisValue)

用法:

var arr = [1,2,3,4,5,6,7];
var ar = arr.map(function(elem){
    return elem*4;
 });
console.log(ar);//[4, 8, 12, 16, 20, 24, 28]
console.log(arr);//[1,2,3,4,5,6,7]
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值