Home > Enterprise >  Node: repeating a method by iterating on js object keys
Node: repeating a method by iterating on js object keys

Time:01-28

I have an object as follows. The value of each key can be null or a string.

  filtObj = {
    currEmployer: null,
    currPosition: null,
    sector: null,
    functn: null,
    subfunctn: null,
    lastStatus: null,
    fName: null,
    lName: null,
    city: null,
    basicQual: null,
    profQual: null
   };

In order to create the filter query for mongo, I need to check the value of each key and perform the below code. The below code is for currEmployer, which needs to be repeated for each key.

  let filtArray = [];
  let isFiltObjNull = true;

  // REQUIRE a LOOP here
    if(filtObj.currEmployer != null) {    // need filtObj.currEmployer as a var
      isFiltObjNull = false;

      const fText = filtObj.currEmployer;     // need filtObj.currEmployer as a var
      let commaFound = true;
      if(fText.search(',') === -1) {
        commaFound = false;
      }

      if(commaFound) myArray = this.fText.split(',');

      if(!commaFound) {
        const x = { currEmployer: { $regex: fText, $options: 'i' } };   // need currEmployer as a var
        filtArray.push(x);

      } else if(myArray.length === 1) {
        let xText = myArray[0].trim();
        if(xText) {
          const x = { currEmployer: { $regex: xText, $options: 'i' } };    // need currEmployer as a var
          filtArray.push(x);
        }

      } else if(myArray.length > 1){
        let subArray = []
        for(let i = 0; i < myArray.length; i   ) {
          let xText = myArray[i].trim();
          if(xText) {
            const x = { currEmployer: { $regex:  xText, $options: 'i' } };   // need currEmployer as a var
            subArray.push(x);
          }
        }
        
        if(subArray.length > 0) {
          const x = { $or: subArray }
          this.filtArray.push(x);
        }
      }
    }

  // LOOP

// IMP NOTE : filtArray will be used within a mongoose query
      docQuery = Candidate.find({
            $and: filtArray
        }).collation({ locale: 'en'});

Basically, is it possible to write filtObj.currEmployer like a variable and iterate over all keys of filtObj

CodePudding user response:

Basically you need a function and instead of hardcoding filtObj.currEmployer you will need filtObj[functionArgumentKey].

By using Object.entries(filtObj).forEach(([key, value]) => {}); it's as simple as just reading the key and value

const filtObj = {
    currEmployer: null,
    currPosition: null,
    sector: null,
    functn: null,
    subfunctn: null,
    lastStatus: null,
    fName: null,
    lName: null,
    city: null,
    basicQual: null,
    profQual: null
};

Object.entries(filtObj).forEach(([key, value]) => {

   // your logic goes here
   console.log(key, value);

});

CodePudding user response:

You can wrap the code into a function and call it by its keys

var object = {
    filtCurrEmployer: "uuu,dd ",
    filtCurrPosition: null,
    filtSector: "hdhhd, dgdg",
    filtFunctn: null,
    filtSubfunctn: null,
    filtLastStatus: null,
    filtFName: null,
    filtLName: null,
    filtCity: null,
    filtBasicQual: null,
    filtProfQual: null
};



function filterSanitizer(key) {
    let filtArray = [];
    if (object[key] != null) {    // need object.key as a var
        const fText = object[key];     // need object.key as a var
        let commaFound = true;
        if (fText.search(',') === -1) {
            commaFound = false;
        }

        if (commaFound) myArray = fText.split(',');

        if (!commaFound) {
            const x = { currEmployer: { $regex: fText, $options: 'i' } };   // need currEmployer as a var
            filtArray.push(x);

        } else if (myArray.length === 1) {
            let xText = myArray[0].trim();
            if (xText) {
                const x = { currEmployer: { $regex: xText, $options: 'i' } };    // need currEmployer as a var
                filtArray.push(x);
            }

        } else if (myArray.length > 1) {
            let subArray = []
            for (let i = 0; i < myArray.length; i  ) {
                let xText = myArray[i].trim();
                if (xText) {
                    const x = { currEmployer: { $regex: xText, $options: 'i' } };   // need currEmployer as a var
                    subArray.push(x);
                }
            }

            if (subArray.length > 0) {
                const x = { $or: subArray }
                filtArray.push(x);
            }
        }
        return filtArray
    } else return undefined

}
const filterQuerry = {}
Object.keys(object).forEach((key) => {
    const r = filterSanitizer(key);
    if (r) {
        filterQuerry[key] = r
    }
})
console.log(object)
console.log(filterQuerry)

  •  Tags:  
  • Related