您好,欢迎访问一九零五行业门户网

js中对数组进行处理两种函数介绍( filter()函数和indexOf()函数)

本篇文章给大家带来的内容是关于js中对数组进行处理两种函数介绍( filter()函数和indexof()函数),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
1. 去重、过滤。
/* * @interface grid 1.行更新 * */ handlegridrowsupdated = ({fromrow, torow, updated })=> { console.log('1.handlegridrowsupdated',arguments) let rows = this.state.rows; for (let i = fromrow; i <= torow; i++) { let rowtoupdate = rows[i]; let updatedrow = react.addons.update(rowtoupdate, {$merge: updated}); rows[i] = updatedrow; } this.setstate({ rows }); }; /* * @interface 2.选中行接口 * @param {array} 选中的行 * */ onrowsselected = (rows ) =>{ /*新增选择的行*/ //进行复选。 this.setstate( { selectedids: this.state.selectedids.concat( rows.map(r => r.row[this.props.rowkey])), }); this.setstate({ selectedrows: this.state.selectedrows.concat( rows.map(r => r.row)), }); //进行单选 /* this.setstate({ selectedids: rows.map(r => r.row[this.props.rowkey]), }); this.setstate({ selectedrows: rows.map(r => r.row), });*/ }; /* * @interface 3.取消选中行接口 * @param {array} 取消选中的行 * */ onrowsdeselected = (rows /*取消选择的行*/) =>{ let rowids = rows.map(r => r.row[this.props.rowkey]); this.setstate({ selectedids: this.state.selectedids.filter(i => rowids.indexof(i) === -1 ), // }); this.setstate({ selectedrows: this.state.selectedrows.filter(r => rowids.indexof(r[this.props.rowkey]) === -1 ) }); }; /** * @interface 4.行选中接口 * */ onrowclick = (rowidx, clickedrow)=>{ //console.log('选中行', rowidx,'_行数据 ', clickedrow); // case1. 如果是全选操作,跳过会自动调用onrowsselected方法,如果是单选操作请隐藏全选checkbox视图操作 if(rowidx === -1){ return; } // case2. 不是全选操作 const hasselected = this.state.selectedrows.some((item)=>{ return item[this.props.rowkey] === clickedrow[this.props.rowkey] }); if(hasselected){ let rowids = clickedrow[this.props.rowkey]; //console.log('选中rowids', rowids ); this.setstate({ selectedids: this.state.selectedids.filter(i => rowids.tostring().indexof(i) === -1 ) }); this.setstate({ selectedrows: this.state.selectedrows.filter(r => rowids.tostring().indexof(r[this.props.rowkey]) === -1 ) }); }else{ // case2-case1. 采用赋值,如果是只能选中一行采用赋值,如果是合并选中行,采用concat方法来合并操作 this.setstate({selectedids: [clickedrow[this.props.rowkey]]}); this.setstate({selectedrows: [clickedrow]}); } };
2. 遍历数组。
{ title: '类型', dataindex: 'type', key: 'type', width: 80, sortable: false, render: (value, record, i)=>{ //类型模块 let _arr =[], text=''; _arr = searchtextbyvalues('doctor_advice_type');//模块名称---匹配字典表。 if(_arr.length > 0) { _arr.map((item)=> { if (value === item.value) { text = item.text; return } }) } return ( <span>{text}</span> ) }},
<===================== viewlabprojectmodal ==========================================>
/** 2.2 字典表的模块类型 **/matchdictionarylist = (type, value)=>{ let moduletype = searchtextbyvalues(type); return ( moduletype instanceof array ? moduletype.map((item)=>{ if(item.value === value) { //匹配 return item.text; } }) : '' )}
使用:
<td><p classname={`${inlinetrcls}`}>{ this.matchdictionarylist('project_detail_type', k.type)}</p></td><td><p classname={`${inlinetrcls}`}>{k.agemin}- {k.agemax}</p> </td><td><p classname={`${inlinetrcls}`}>{ this.matchdictionarylist('sex', k.sex)}</p></td><td><p classname={`${inlinetrcls}`}>{k.minvalue}-{k.maxvalue}</p></td>
相关推荐:
js数组(array)处理函数整理_基础知识
js如何获取数组并处理php数组
以上就是js中对数组进行处理两种函数介绍( filter()函数和indexof()函数)的详细内容。
其它类似信息

推荐信息