Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

107
Vistas
How to move values ​from one array to another in angular 11

I want to move a value on one array to another. Here's the first array

"availableMember": [
      {
        "id": 18,
        "userDivisionId": 1,
        "userDivisionCode": "1",
        "userDivisionName": "DEPT. MARKETING & LOGISTIK"
      },
      {
        "id": 26,
        "userDivisionId": 333,
        "userDivisionCode": "2.3.21.02",
        "userDivisionName": "YGY PO"
      },
      {
        "id": 27,
        "userDivisionId": 335,
        "userDivisionCode": "2.3.21.02.04",
        "userDivisionName": "YGY MO"
      },
    ]

and I want to move, for example object with userDivisionId is 333, to the second array.

"selectedMember": []

I already try using .splice(), but sometimes its work and sometimes it doesn't. Any idea how to do this? Please help me. Thank you.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Array.find() is the most appropriate method. It takes a function as a parameter and will return the first element in which the function returns true. If you want to find all occurrences, just substitute find with filter. Use Array.push() to add an element to an array, or Array.concat() to concatenate two arrays.

 availableMember = [
    {
      id: 18,
      userDivisionId: 1,
      userDivisionCode: '1',
      userDivisionName: 'DEPT. MARKETING & LOGISTIK',
    },
    {
      id: 26,
      userDivisionId: 333,
      userDivisionCode: '2.3.21.02',
      userDivisionName: 'YGY PO',
    },
    {
      id: 27,
      userDivisionId: 335,
      userDivisionCode: '2.3.21.02.04',
      userDivisionName: 'YGY MO',
    },
  ];
  selectedMember: any[] = [];

Adding a single element

  ngOnInit(): void {
    const selected = this.availableMember.find(
      (el) => el.userDivisionId === 333
    );
    this.selectedMember.push(selected);
  }

Adding multiple elements

  ngOnInit(): void {
    const selected = this.availableMember.filter(
      (el) => el.userDivisionId === 333 || el.userDivisionId === 335
    );
    this.selectedMember = this.selectedMember.concat(selected);
  }
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can try something like below, where you can pass division id into the function call like selectMember(333). This is a dynamic approach. You can also transform or overload this function with more inputs to match userDivisionName or id in future

const selectMember = (divisionId) => {
  availableMember.map(function(x) {
    if (x.userDivisionId === divisionId)
      selectedMember.push(x)
  })
}

or variation of the above function

const selectMemberV2= (divisionId) => {
  selectedMember = availableMember.filter(x => x.userDivisionId === divisionId);
}

Working Code

let availableMember = [{
    "id": 18,
    "userDivisionId": 1,
    "userDivisionCode": "1",
    "userDivisionName": "DEPT. MARKETING & LOGISTIK"
  },
  {
    "id": 26,
    "userDivisionId": 333,
    "userDivisionCode": "2.3.21.02",
    "userDivisionName": "YGY PO"
  },
  {
    "id": 27,
    "userDivisionId": 335,
    "userDivisionCode": "2.3.21.02.04",
    "userDivisionName": "YGY MO"
  },
];


selectedMember = [];
// function to select member with different input
const selectMember = (divisionId) => {
  availableMember.map(function(x) {
    if (x.userDivisionId === divisionId)
      selectedMember.push(x)
  })
}

// call 
selectMember(333);

//log
console.log(selectedMember)

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda