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

101
Views
Extender el uso de set para tener múltiples índices

Estoy tratando de descubrir cómo puedo extender mi uso de set y has para poder agregar otros índices al conjunto.

Actualmente, estoy creando un conjunto de identificadores a partir de un objeto existente que funciona bien, pero también me gustaría agregar los otros índices en el conjunto si es posible para poder verificar primero si el conjunto tiene el identificador I estoy buscando, y luego, si es así, puedo hacer una comparación de la cantidad (y eventualmente también otros índices)

¿Hay alguna manera de extender este enfoque para agregar los otros índices al conjunto y seguir usando has de una manera eficaz? Si no, ¿cuál sería un mejor enfoque para lograr lo que quiero aquí?

 const testFunc = (rows) => { let itemSet = new Set(vm.$data.categoryItems.map((category) => category.category_identifier)) category_ID = 1234; if (itemSet.has(category_ID)) { qty = 10; if(qty > itemSet.cagegory_qty){ console.log('good') } } } var vm = new Vue({ data: { parsedResults: [], categoryItems:[ { category_identifier:1234, category_qty:50, available_date:"2021-11-02", }, { category_identifier:2345, category_qty:15, available_date:"2021-11-02", }, { category_identifier:3456, category_qty:25, available_date:"2021-11-02", }, { category_identifier:6533, category_qty:2, available_date:"2021-11-02", } ] }, computed: { getTestSet() { setRows = testFunc(this.rows) this.parsedResults = setRows return this.parsedResults } } })
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

En su lugar, puede usar un Map donde la clave es el identificador y el valor puede ser el objeto si desea usar múltiples atributos en él:

 const itemMap = new Map( vm.$data.categoryItems.map(category => [category.category_identifier, {...category}]) )

Para verificar si el Mapa tiene una determinada clave, puede usar Map#has .

Para obtener un atributo de un determinado objeto por el identificador, puede usar Map#get .

 if (itemMap.has(category_ID)) { const qty = 10; if(qty > itemMap.get(category_ID).category_qty){ console.log('good') } }
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!