单引号转换成双引号
单引号替换成双引号
JSON.stringify(params).replace(/'/g, '"')
数字变成数组123----【1,2,3,4】
String(123).split("")
数组【1,2,3,4】变成数字
【1,2,3,4】.join(",").replace("/,/g","")*1,
一个数组中含有若干个字符串,根据不同的key值给字符传后添加文字(例如年月日。。),不用多个if实现
动态取值简单方法,直接在源头
const date = ["日", "周", "月", "年"];
data.forEach(
({ abscissaName, ordinateFirstValue, ordinateSecondValue }) => {
xData.push(
`${abscissaName.substr(abscissaName.length - 2)}${date[type - 1]}`
);
yData.push(ordinateFirstValue);
y2Data.push(ordinateSecondValue);
}
);
不太简单的方法
data.forEach(
({ abscissaName, ordinateFirstValue, ordinateSecondValue }) => {
xData1.push(abscissaName);
yData.push(ordinateFirstValue);
y2Data.push(ordinateSecondValue);
}
);
const date = ["日", "周", "月", "年"]; //把要添加的值放在一个数组里面
const xData = xData1.map(
string => `${string.substr(string.length - 2)}${date[type - 1]}`
); //取两位字符串,动态取数组中的值,type是动态的值
if麻烦的实现
data.forEach(
({ abscissaName, ordinateFirstValue, ordinateSecondValue }) => {
switch (type) {
case 1:
xData.push(`${dataListLast(abscissaName)}日`);
break;
case 2:
xData.push(`${dataListLast(abscissaName)}周`);
break;
case 3:
xData.push(`${dataListLast(abscissaName)}月`);
break;
case 4:
xData.push(`${dataListLast(abscissaName)}年`);
break;
default:
break;
}
yData.push(ordinateFirstValue);
y2Data.push(ordinateSecondValue);
}
);
map
map方法我们用得最最最多了吧,当你需要批量改变数组中的值的时候就要想到它了
注:map方法不会改变原始值,它会返回一个新的数组
let arr = [1, 2, 3, 4, 5, 6]
// 把上面的arr中数据统一加2
arr = arr.map(val => val + 2)
console.log(arr) // [3, 4, 5, 6, 7, 8]
let arr = [{
age: 25,
address: '你猜'
}, {
age: 40,
address: '你猜'
}, {
age: 30,
address: '你猜'
}]
// 此时我们要把年龄都加上2
arr = arr.map(item => item.age += 2 )
arr = arr.map(item => {
item.age += 2
return item
})
console.log(arr[0].age) // 27
// 不同值返回不同的map
param.photo = JSON.stringify(
imgList.map((item) => {
if (item.response) {
const {
data: { url },
} = item.response;
return url;
}
return item.url;
})
);
} else {
param.photo = null;
}
将A对象数组中某个属性存到B数组中
var building=[
{name:'the Great Wall',location:'BeiJing'},
{name:'Eiffel Tower',location:'Paris '}
]
var citys=building.map(item=>{
return item.location
})
console.log(citys) //["BeiJing", "Paris "]
filter
1: filter() 去掉空字符串、undefined、null
2: 当我们需要对数组进行筛选的时候
注:此方法也不会改变原始数组,会返回一个筛选成功匹配的元素组成的新数组。
let arr = [{
age: 25,
address: '你猜'
}, {
age: 40,
address: '你猜'
}, {
age: 30,
address: '你猜'
}]
// 此时我们要把年龄大于30的人筛选出来
let newArr = arr.filter(item => {
return item.age > 30
})
console.log(newArr.length) // 1
console.log(newArr[0].age) // 40
假定有两个数组(A,B),根据A中id值,过滤掉B数组不等于A中id的数据
var post = {id:4,title:"Javascript"};
var comments = [
{postId:4,content:"Angular4"},
{postId:2,content:"Vue.js"},
{postId:3,content:"Node.js"},
{postId:4,content:"React.js"}
];
function commentsForPost(post,comments){
return comments.filter(function(comment){
return comment.postId === post.id;
})
}
console.log(commentsForPost(post,comments)); //[ {postId:4,content:"Angular4"},{postId:4,content:"React.js"}]
filter过滤二维数组
let data = [
{
a: [{ re: true }, { re: false }],
b: '123',
},
{
a: [{ re: true }, { re: false }],
b: '123',
},
];
// 借助forEach解决最外重循环
let newArray = [];
data.forEach((now, index) => {
newArray[index] = now;
newArray[index].a = now.a.filter(item => {
return item.re === true;
});
});
console.log(newArray);
find
var arr = [1,2,3,4,5,6] ;
var new1 = arr.find(function(item,index){
return item>2&&item<4 ; //当遍历循环到判断到一个为true则跳出循环,输出当前数组元素,不再循环
})
var new2 = arr.find(function(item,index){
return item.toString().indexOf(5)>-1 ; //把当前数组元素转为字符串,则可index()>-1判断是否含有某字符
})
console.log(new1); //打印操作后的结果
console.log(new2) //打印是否含有某字符
console.log(arr); //打印原数组,find()没有改变原数组
根据id获取数组对应的值(适合数组中套数组,例如级联----省市县区)
const findName = (
arr,
theCode,
codeKey = "id", ****多维数组时更改字段
valueKey = "name", ****多维数组时更改字段
childKey = "children" ****多维数组时更改字段
) => {
for (let i = 0; i < arr.length; i += 1) {
const item = arr[i];
const id = item[codeKey];
const name = item[valueKey];
const children = item[childKey];
if (id === theCode) {
return name;
}
if (children && children.length > 0) {
const res = findName(children, theCode);
if (res) {
return res;
}
}
}
};
const getNameByCode = (code) => {
return findName(pca, code);
};
getNameByCode(对应的id或是code值)