反应过滤器,然后交换数组元素

时间:2018-12-11 05:59:48

标签: javascript reactjs

private List<DateTime> dates;
private BindingList<DateTime> bDates;
private BindingSource dSource;

private DataTable dataTable = new DataTable();

public Form1() {
  InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e) {
  dates = new List<DateTime>();
  dtpDate.Format = DateTimePickerFormat.Custom;
  dtpDate.CustomFormat = "dd/MM/yyyy";
  dtpTime.Format = DateTimePickerFormat.Custom;
  dtpTime.CustomFormat = "hh:mm";
  dtpTime.ShowUpDown = true;

  dataTable.Columns.Add("Date", typeof(DateTime));
}

private void button1_Click(object sender, EventArgs e) {
  DateTime input = dtpDate.Value.Date + dtpTime.Value.TimeOfDay;
  MessageBox.Show(input.ToString()); //This shows the date correctly
  dates.Add(input);
  bDates = new BindingList<DateTime>(dates);
  dSource = new BindingSource(bDates, null);
  grid.DataSource = dSource;
  grid.Columns[0].DefaultCellStyle.Format = "dd/MM/yyyy HH:mm";

  // using datatable on second grid
  dataTable.Rows.Add(input);
  grid2.DataSource = dataTable;
  grid2.Columns[0].DefaultCellStyle.Format = "dd/MM/yyyy hh:mm:ss tt";
}

例如,我有一个 const filterData = apiData.filter(data => { return this.shouldDisplayItem( data, [this.state.searchValue], this.state.filterKeyValue ); }).filter(i => i.vid), x = 0, y = apiData.map(i => i.vid).indexOf(markerId); A[x] = A.splice(y, 1, A[x])[0];。首先,我要过滤大于2的值,然后通过索引号交换7和8。

首先在原始项目中,我正在做一些过滤,而不是在第二个过滤中,我交换了两个数组对象 我们可以一次过滤两次相同的数组吗?

2 个答案:

答案 0 :(得分:1)

您可以使用filter过滤掉数组,然后使用prototype

swap

答案 1 :(得分:1)

您可以使用reduce()简单完成

所以在reduce函数中,我首先检查value > 2条件。

  • 如果通过,则我正在检查value === 7value===8是否与它们匹配,我将根据需要更改值。否则,我将直接推入输出数组。
  • 如果value > 2失败,我不会在输出数组中推送该值。

let arr = [0,1,2,3,4,5,6,7,8,9];
let op = arr.reduce((op,cur)=>{
  if(cur>2){
    if( cur ===7)op.push(8);
    else if(cur === 8) op .push(7);
    else op.push(cur);
  }
  return op;
},[])

console.log(op);

相关问题