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

93
Vistas
Two forms of a copy function in javascript

There are usually two ways to copy an object. For example, using the C model, one way is you'd pass back a new object, and the second way is you'd write data to an existing object/address. Here are two ways to simulate doing that in JS:

function copy_array(arr) {
    // copy array and return existing array
    const cp = [];
    for(let i=0; i < arr.length; i++)
        cp[i] = arr[i];
    return cp;
}
function copy_to(arr, to_arr) {
    // copy to existing object
    // possible to write to a "reference object", something like `&to_arr`
    to_arr = []
    for(let i=0; i < arr.length; i++)
        to_arr[i] = arr[i];
    return to_arr;
const c = ['a','b','c'];
let arrc=copy_array(c);
const x=[];
copy_to(c, x);

Is it possible to copy objects these two ways? Or is copying to a reference object/address not possible?

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

0

For an object you do it essentially the same way, except you use for (key in object) to iterate over the property names.

function copy_to(obj, to_obj) {
  for (let key in obj) {
    to_obj[key] = obj[key];
  }
  return to_obj;
}
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