// 不确定是有几个tab页,arr里面
// 下面的对象是每个tab页对应的下标
let arr = ['01','03']
let n = {}
for (var i = 0; i < arr.length; i++) {
console.log(i,arr[i])
// 0 '01'
// 1 '03'
n[arr[i]]=i
}
console.log(n)
// {
// "01": 0,
// "03": 1
// }
let imgs = {
0: ['lego/0.jpg'],
1: ['lego/1.jpg'],
3: ['lego/3.jpg'],
}
let ls = Object.keys(imgs).map((colorId) => ({
ls: imgs[colorId],
id: +colorId,
}));
//console.log(Object.keys(imgs))
['0', '1', '3']
//console.log(JSON.stringify(ls))
[
{ls: ["lego/0.jpg"],id: 0},
{ls: ["lego/1.jpg"],id: 1},
{ls: ["lego/3.jpg"],id: 3}
]
从数组中删除项.
function remove (arr, item) {
if (arr.length) {
var index = arr.indexOf(item);
if (index > -1) {
return arr.splice(index, 1)
}
}
}
取数组中的value值组成新的数组
let nameArr=[],ageArr=[];
let arr1=[
{id:'11',height:153},
{id:'12',height:153},
{id:'13',height:153},
]
arr1.forEach(item=>{
nameArr.push(item.id);
ageArr.push(item.height);
})
console.log(nameArr); ['11','12','13']
console.log(ageArr); [153,153,153]
let arr=[
{id:'11',height:153},
{id:'12',height:153},
{id:'13',height:153},
]
const arra=arr.map(item=>{
return item.height;
})
console.log(arra);
[153, 153, 153]
取特定值组成新的数组
let textArr=[
{id:'11',sex:"000",age:'331'},
{id:'12',sex:"001",age:'332'},
{id:'13',sex:"002",age:'333'},
]
let newArr=textArr.map(item=>{
return Object.assign({},{
id:item.id,
sex:item.sex,
})
})
console.log(newArr);
[{id: '11', sex: '000'},{id: '12', sex: '001'},{id: '13', sex: '002'}]
异步执行顺序问题
let flag=3
if(flag==3){
setTimeout(
item=>{
console.log('111111111111');
},3000);
console.log('222');
}
if(flag==3){
setTimeout(item=>{console.log('0000000000000');},2000)
}
222
0000000000000
111111111111
async function fn(){
console.log('2220');
var result = await fn1()
console.log('3330');
return result
}
function fn1(){
let aa='333';
console.log('1110');
return aa;
}
fn()
01.html:71 2220
01.html:78 1110
01.html:73 3330
function fn1(){
setTimeout(item=>{console.log('fn1');},3000)
}
function fn2(){
setTimeout(item=>{console.log('fn2');},2000)
}
fn1();
fn2();
// fn2
// fn1
加粗样式
let obj={
id:"",
age:"14"
}
console.log(Object.values(obj));
console.log(Object.values(obj).length);
let arr333=Object.values(obj).filter(item=>{
return item!=''
})
console.log(Object.values(obj),'00000'); // ['', '14']
console.log(arr333,'11111111111111'); // ['14']
let radarOriginData = [
{
name: '个人',
ref: 10,
koubei: 8,
},
{
name: '团队',
ref: 3,
koubei: 9,
},
{
name: '部门',
ref: 4,
koubei: 1,
},
];
let radarData = [];
radarOriginData.forEach((item) => {
Object.keys(item).forEach((key) => {
if (key !== 'name') {
radarData.push({
name: item.name,
value: item[key],
});
}
});
});
// [
// {name: '个人', value: 10},
// {name: '个人', value: 8},
// {name: '团队', value: 3},
// {name: '团队', value: 9}
// {name: '部门', value: 3},
// {name: '部门', value: 9}
// ]
const offlineData = [];
for (let i = 0; i < 3; i += 1) {
offlineData.push({
name: `门店${i}`,
cvr: Math.ceil(Math.random() * 9) / 10,
});
}
console.log(offlineData);
// [
// {name: '门店0', cvr: 0.7},
// {name: '门店1', cvr: 0.4},
// {name: '门店2', cvr: 0.2},
// ]
const type = [
{key:'name',value:'测试数据'},
{key:'code',value:'TS'},
{key:'type',value:'类型'},
{key:'is_halt',value:'是否停用'}
]
let condition={};
type.map(itm=>{
condition[itm.key] = itm.value
})
console.log(condition);
// {name: '测试数据', code: 'TS', type: '类型', is_halt: '是否停用'}
let peo=[
{age:'111',height:'190'},
{age:'4111',height:'1950'},
{age:'1511',height:'1690'},
]
let a=[];
peo.forEach(item=>{
let aa=Object.values(item);
a.push(aa);
})
console.log(a,'aa');
// [
// ['111', '190'],
// ['4111', '1950'],
// ['1511', '1690'],
// ]
if(true){
setTimeout(item=>{console.log(111)},4000)
}
setTimeout(item=>{console.log(1111)},3000)
1111
111
for循环删除页面上的元素
html:
<div style="display:flex" *ngFor="let i of list1; let idx = index">
<p>{{i.title}}</p>
<button nz-button (click)="dd(i.id,idx)">点击</button>
</div>
list1:any[]=[
{title:"丽丽1",id:10},
{title:"丽丽2",id:20},
{title:"丽丽3",id:30},
]
dd(id: number, idx: number){
this.list1.splice(idx, 1);
console.log('333');
}
list=[
{id: 2, parent_id: 0, type: 'folder', ext: 'folder', title: '委看分北期'},
{id: 3, parent_id: 0, type: 'folder', ext: 'folder', title: '特气划则'},
{id: 4, parent_id: 0, type: 'folder', ext: 'folder', title: '及在产收们'},
]
// splice会改变原数组
// 开始的下标 删除的个数 删除后插入的元素
// 返回删除的元素,原数组会发生变化
let arr=list.splice(0, 0, { id: 0, title: '根目录1' },{ id: 1, title: '根目录2' });
console.log(arr); // length 0
console.log(list,'0000');
// list=[
// { id: 0, title: '根目录1' },
// { id: 1, title: '根目录2' },
// {id: 2, parent_id: 0, type: 'folder', ext: 'folder', title: '委看分北期'},
// {id: 3, parent_id: 0, type: 'folder', ext: 'folder', title: '特气划则'},
// {id: 4, parent_id: 0, type: 'folder', ext: 'folder', title: '及在产收们'},
// ]
这是确定知道数组中某个位置有id 为12的值,要不然,找不到的话,就把最后一个给删除了
id=12;
a=[
{id: 10, parent_id: 2, type: 'file', title: '电值直规区.png',},
{id: 11, parent_id: 2, type: 'file', title: '织型干.png',},
{id: 12, parent_id: 2, type: 'file', title: '织型1干.png',},
]
this.a.splice(
this.a.findIndex((w) => w.id === id),
1,
);
console.log(this.a);
// a=[
// {id: 10, parent_id: 2, type: 'file', title: '电值直规区.png',},
// {id: 11, parent_id: 2, type: 'file', title: '织型干.png',},
// ]
//取消
check(id){
this.todos.forEach((todo)=>{
if(todo.id===id) todo.done=!todo.done
})
}
删除
delete(id){
this.todos=this.todos.filter(todo=>todo.id!==id)
}
delete(id){
this.todos=this.todos.filter(todo=>{
return todo.id!==id
})
}
reduce
// reduce
function fn(){
// 上一次的值 当前的值
// pre 第二次调用时候的值,是第一次调用函数的返回值
// 由于没有返回值 就是undefined
this.todos.reduce((pre,current)=>{
console.log('@',pre);
// @ 0
// @ undefined
// @ undefined
},0)
}
fn();
// 高端写法 reduce专门用于做条件统计的
let todos=[
{id:'001',title:'抽烟1',done:true},
{id:'002',title:'抽烟2',done:false},
{id:'003',title:'抽烟3',done:true},
]
// 最后一次调用函数的返回值就是这个函数的返回值
let aa=todos.reduce((pre,current)=>{
let idx=current.done==true ? 1 : 0
return pre+idx
},0)
console.log(aa,'111')
// 0 1
// 1 1
// 1 2
let aa=todos.reduce((pre,current)=>pre + (current.done==true ? 1 : 0),0)
如果值为[],则移除
false.0、空字符串(“”)、NaN、null 和 undefined 被转换为 false
deleteChildren(arr) {
arr.forEach(val=>{
if(val.children){
if(val.children.length){
this.deleteChildren(val.children)
}else{
delete val.children
}
}
})
return arr
},
list: [
{
value: '1',
children: []
},
{
value: '2',
children: null
},
]
// 如果值为[],则移除
console.log(this.deleteChildren(this.list))
// list: [
// {
// value: '1',
// },
// {
// value: '2',
// children: null
// },
// ]
let arr = {
"1": {
"1101": {
"code": "1101",
"name": "测试1",
},
"code": "11",
"name": "测试1",
}
}
for (let pro in arr){
console.log(pro,arr[pro])
}
// 1
// {
// "1101": {
// "code": "1101",
// "name": "测试1"
// },
// "code": "11",
// "name": "测试1"
// }
let activeData = [];
for (let i = 0; i < 24; i += 1) {
activeData.push({
x: i,
y: i * 50 + Math.floor(Math.random() * 200),
});
}
this.activeData = activeData;
const status = [
{
"index": 0,
"text": "运行1",
"checked": true
},
{
"index": 2,
"text": "运行3",
"checked": true
}
]
let statusList = status.filter((w) => w.checked).map((item) => item.index);
console.log(statusList,'222'); // [0, 1]
const status = [
{
"index": 0,
"text": "关闭",
"checked": true
},
{
"index": 1,
"text": "运行中",
"checked": true
}
]
let statusList = status.filter((w) => !w.checked).map((item) => item.index);
console.log(statusList,'222'); // []
const status = []
let statusList = status.filter((w) => !w.checked).map((item) => item.index);
console.log(statusList,'111'); // []
// Uncaught TypeError: Cannot read properties of null (reading 'filter')
const status = null
let statusList = (status || []).filter((w) => !w.checked).map((item) => item.index);
console.log(statusList,'111'); // []
const selectedRows = [
{
"index": 0,
"callNo": 1
},
{
"index": 1,
"callNo": 2
}
]
let totalCallNo = selectedRows.reduce((total, cv) => total + cv.callNo, 0);
console.log(totalCallNo) //3
let arr1=selectedRows.map((i)=>{
return Object.assign({},{
he:i.callNo,
})
})
// console.log(arr1);
// [{he: 1},{he: 2}]
let str1
let str = {
'01':{name:"hu"},
'02':{name:"hu1"}
}
for (let pro in str){
console.log(pro,JSON.stringify(str[pro]))
// 01 {"name":"hu"}
// 02 {"name":"hu1"}
}
let arr = {
'1':'test1',
'2':'test2',
'3':'test3'
}
// 未知
let yuan = ['1', '3']
let newArr = yuan.map((rating) => arr[rating]);
// ['test1','test3']
//创建一个验证方法
const validates = (item) => {
return new Promise((resolve, reject) => {
if (item.indexOf('1')>-1) {
resolve()
} else {
reject()
}
})
}
Promise.all(newArr.map((item) => validates(item))).then((res)=>{
console.log(res,'suc')
//提交数据
}).catch((res)=>{
console.log(res,'err')
//提交数据
})