Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

132
Visualizações
Assign each value to object

I am taking data from my backend in this simple way

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 is a Object I am getting reservation number from form then I want to slice everything that I don't want. ( Regex works fine ). Then I want to assign this prepared value into my this.form then I using *ngFor in table

How can I assign each value from forEach loop into this.form

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

As read in the documentation, the js slice method

Return A new array containing the extracted elements.

In fact, the slice methods does not modifiy the current element of the array


To solve your problem, you can either use the splice methods which works as the following :

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('/');
    });

Or re-assign the value using the slice method

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

Small example of usage

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}`)

about 4 years ago · Juan Pablo Isaza Relatório

0

Propose of .foreach() doesn't serve what you want.

However, If you feel the life is quite ordinary and want to do some advance. Check is out Array.prototype.forEach()

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda