数组操作 filter map
删除数组指定key值
1. 数组是在页面定义,不是从api获取:
const arrayData= [{index: 0,is_required: true,name: "vmvdnksl",type: "LONG_TEXT"},{index: 1,is_required: true,name: "dsvnlk",type: "MULTIPLE_SELECTORS"}]; const result = arrayData.map(({type,index,...rest}) => ({...rest})); console.log(result);
2. 数组格式未知,是从api获取的数据
var arrayData = { 0: { index: 0, is_required: true, name: "vmvdnksl", type: "LONG_TEXT" }, 1: { index: 1, is_required: true, name: "dsvnlk", type: "MULTIPLE_SELECTORS" } }; for (let key in arrayData) { delete arrayData[key].index; delete arrayData[key].type; } console.log(arrayData);
对比a、b两个数组,将a不在b数组里面的选项push到b数组去
a数组: data1:[ {id:'1',name: 'POEM > 10', date: '8/16/21', action: '0'}, {id:'2',name: 'Patient had 4 flare-ups in the last 6 months', date: '8/16/21', action: '1'}, {id:'3',name: 'BSA > 10', date: '8/16/21', action: '0'}, {id:'4',name: 'Patient had surgery', date: '8/16/21', action: '0'}, {id:'5',name: 'Upcoming appointment date is in the past', date: '8/16/21', action: '0'}, {id:'6',name: 'Patient reported Plaque Psoriasis has slightly worsened', date: '8/16/21', action: '0'} ]; b数组: data2: [ {id:'7',name: 'POEM > 10', date: '8/16/21', action: '0'}, {id:'8',name: 'Patient had 4 flare-ups in the last 6 months', date: '8/16/21', action: '1'}, ]; let filterData=this.data1.filter((item)=>{ return this.data2.filter(data=>data['id']==item['id']).length==0 }) this.data2.push(...filterData)
数组二次filter循环
let newArr = [] this.patientReportDatas.forEach((item) => { let res = item['surveyQuestionsResponses'].filter((j) => j['externalQuestionID']=='36770' || j['externalQuestionID']=='36792') console.log(res) if(res && res.length) { item['surveyQuestionsResponses'] = res newArr.push(item) } })

浙公网安备 33010602011771号