将每个值分配给对象

Assign each value to object

我正在以这种简单的方式从我的后端获取数据

this.archiveService.getRooms(team).subscribe(
  res => {
   this.form = res;
    this.form.forEach(el => {
     el.reservation.slice(-6).match(/.{1,2}/g).join('/');
    });

  },
  error => {
    console.log(error);
  }
)

this.form 是一个对象,我从表单中获取预订号,然后我想切掉所有我不想要的东西。 (正则表达式工作正常)。 然后我想将这个准备好的值分配到我的 this.form 然后我在 table

中使用 *ngFor

如何将 forEach 循环中的每个值分配给 this.form

.foreach() 的求婚不符合您的要求。

但是,如果你觉得生活很平凡,想做一些进步。 支票已出 Array.prototype.forEach()

如文档中所述,js slice 方法

Return A new array containing the extracted elements.

事实上,切片方法不会修改数组的当前元素


要解决您的问题,您可以使用 splice 方法,其工作原理如下:

The splice() method changes the contents of an array by removing or replacing existing elements and/or adding new elements in place. To access part of an array without modifying it, see slice().

this.form.forEach(el => {
     el.reservation.splice(-6).match(/.{1,2}/g).join('/');
    });

或re-assign使用slice方法的值

this.form.forEach(el => {
     el.reservation = el.reservation.slice(-6).match(/.{1,2}/g).join('/');
    });

使用的小例子

const myArr = [1,2,3,4,5,6,7,8,9,10]

const returnVal = myArr.slice(0,5)

console.log(`val return by slice : ${returnVal}`)
console.log(`current arr : ${myArr}`)

console.log('------------------')


const myArr2 = [1,2,3,4,5,6,7,8,9,10]

const returnVal2 = myArr2.splice(0,5)

console.log(`val return by splice : ${returnVal2}`)
console.log(`current arr : ${myArr2}`)