Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

169
Views
Obtener el valor del objeto principal

¿Hay alguna forma de obtener un valor clave del objeto principal de un objeto? En el siguiente ejemplo, quiero combinar urlParent con la section :

 const linkItems = [ { id: 1, name: 'Home Page', urlParent: '/home', subItems: [ { subId: 1, name: 'Project 1', section: '#project1', get url() { //output /home#project1 } } ] } ]; console.log(linkItems[0].subItems[0].url) // /home#project1;
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

No puede hacer referencia a un Objeto principal de esa manera (Objeto (valor) ← Matriz ← Objeto), y ni siquiera desde el Objeto principal de un Objeto.
Lo que puedes hacer en su lugar es:

  • Cree dos Clases , una para el padre y otra para el hijo.
  • Al agregar un hijo al parent , simplemente haga una "lista vinculada" , haciendo referencia a this propiedad principal del elemento secundario creado.

 class Child { constructor(data) { Object.assign(this, data); } get url() { return this.parent.urlParent + this.section } } class Parent { constructor(data) { Object.assign(this, data); this.subItems = []; } addChild(item) { this.subItems.push(new Child({...item, parent: this})); } } // Example: const parent = new Parent({id:1, name:"Home", urlParent:"/home"}); parent.addChild({subId:1, name:"Project 1", section:"#project1"}); console.log(parent.subItems[0].url) // /home#project1;

¡Pero hey! Nodos y árboles

Su idea original y la anterior utilizan demasiada complejidad.
Lo que sugeriría es tratar a todos los padres, hijos, lo que sea, como nodos de página .

 class Page { constructor(data) { Object.assign(this, data); this.children = {}; } addChild(page) { page.parent = this; // Linked to parent! this.children[page.id] = page; } get url() { // Generate full URI by recursing the parents tree return this.parent ? `${this.parent.url}/${this.slug}` : this.slug; } } // Example: // 1. Create pages: const pageRoot = new Page({id:1, name:"Home page", slug:""}); const pageProj = new Page({id:3, name:"All projects", slug:"projects"}); const pageWebs = new Page({id:4, name:"Websites", slug:"websites"}); const pageStOv = new Page({id:6, name:"Stack Overflow", slug:"so"}); const pageSpec = new Page({id:9, name:"Stack Overflow Specs", slug:"specs"}); // 2. Create Tree: pageRoot.addChild(pageProj); pageProj.addChild(pageWebs); pageWebs.addChild(pageStOv); pageStOv.addChild(pageSpec); // 3. Test console.log(pageRoot.url); // ""; console.log(pageProj.url); // "/projects"; console.log(pageSpec.url); // "/projects/websites/so/specs"; console.log(pageRoot);

about 4 years ago · Juan Pablo Isaza Report

0

const linkItems = [ { id: 1, name: 'Home Page', urlParent: '/home', get subItems(){ console.log(this.name); return ([ (() => { console.log(this); return { subId: 1, name: 'Project 1', section: '#project1', urlParentFromOuterScope: () => { return this.urlParent; }, sectionString(){ return this.section; }, url(){ console.log('url', this); return this.urlParentFromOuterScope() + this.sectionString(); } } })() ]) } } ]; const subItems = linkItems[0].subitems; console.log(linkItems[0].subItems[0].url());

Siéntase libre de eliminar los 'console.log's innecesarios después de comprender el enfoque. Me tomé la libertad de agregar algunos métodos. Esta es complicada y tiene que ver con el alcance de esto en las funciones de matriz. PD: Supongo que esto se puede simplificar.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!