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

166
Views
¿Javascript crea expresiones regulares para el filtro?

Estoy recibiendo un objeto como este desde la aplicación de front-end:

 { name: "Red boys" category: "Fashion" subCategory: [ { name: "sub"}, { name: "Hub"} { name: "Mub"} // Here it is not static, Here can more or less object in the array ] }

Tengo que crear un nuevo objeto a partir de él:

 { category: {name: "Fashion", subCategory: {name: {$in: ["sub", "Hub", "Mub"]}}}}

Aquí está mi código-

 let args = {}; for (let key in filters) { if (filters[key].length > 0) { if (key === "category") { args["category"] = { name: filters['category'], // Here I have to check if subCatgory has some value, then create subcategory field subCategory: { name: { $in: [] // Here I can't what I have to wrie } } } } } } console.log(args);

Por favor ayúdame. No puedo entender esta funcionalidad. Busco pero puedo conseguir alguna solución.

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

0

Puede desestructurar el objeto original y luego reestructurar las propiedades de él en un nuevo objeto. Para las subcategorías, puede map sobre la matriz para crear una matriz de objetos.

 const fe = { name: 'Red boys', category: 'Fashion', subCategory: [ { name: 'sub' }, { name: 'Hub' }, { name: 'Mub' } ] }; // Get the category and subcategory from the // object passed from the front-end app const { category, subCategory } = fe; // And create a new object const be = { category: { name: 'Fashion', subCategory: { name: { $in: subCategory.map(s => s.name) } } } }; console.log(be);

about 4 years ago · Juan Pablo Isaza Report

0

Aquí está la solución simple al objetivo:

 out.subCategory.name['$in'] = in.subCategory.map( item => item.name ); 

 const inputData = { name: "Red boys", category: "Fashion", subCategory: [ { name: "sub"}, { name: "Hub"}, { name: "Mub"} ] }; function convertToDesired ( obj ) { const output = { category: { name: obj.category, subCategory: { name: { $in: obj.subCategory.map( item => item.name ) } } } }; return output; } console.log(convertToDesired(inputData))

about 4 years ago · Juan Pablo Isaza Report

0

@Suvana Según su implementación, así es como puede lograr el resultado deseado. Adjunté el código de violín también.

https://jsfiddle.net/xco7vkn8/1/

 for (let key in filters) { if (filters[key].length > 0) { if (key === "category") { args["category"] = { name: filters['category'], subcategory:{}, } } else if(key ==="subCategory"){ if(filters['subCategory'].length>0){ new_array = []; filters['subCategory'].forEach(function (item, index) { new_array.push(item.name); }); args["category"].subcategory={ name:{ $in:new_array } } } } } } console.log(args);
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!