Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

172
Visualizações
¿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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda