Home > Software design >  Javascript create regex for filter?
Javascript create regex for filter?

Time:12-06

I'm receiving an object like this from the front-end app:

{
 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
 ]
}

I have to create a new object from it:

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

Here is My Code-

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);

Pleae help me. I can't understand this functionality. I search but I can get any solutions.

CodePudding user response:

You can destructure the original object, and then restructure the properties from it into a new object. For the sub categories you can map over the array to create an array of objects.

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);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

Here is the simple solution to the objective:

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 = {};
  output.category = {};
  output.category.name = obj.category;
  output.category.subCategory = {}; 
  output.category.subCategory.name = {}; 
  // this line is probably is what you looking for
  output.category.subCategory.name['$in'] = obj.subCategory.map( item => item.name );
  return output;
}

console.log(convertToDesired(inputData))
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

@Suvana Based on your implementation here is how you can achieve the desired output. I attached the fiddle code as well.

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);
  • Related