3

This is an object to be processed:

var q = {
    email: {contains: "noname@hotmail.com"},
    name: {contains: "someuser"}
};

I would like to go through each key of q and if the corresponding value is an object that has the property contains then replace it with $regex.

Mamun
  • 66,969
  • 9
  • 47
  • 59
Ali Celebi
  • 824
  • 1
  • 10
  • 19

6 Answers6

2

Related information can be found here: JavaScript: Object Rename Key

You can try the following way:

var q = {
    email: {contains: "noname@hotmail.com"},
    name: {contains: "someuser"}
};
for(var k in q){
  if(q[k].hasOwnProperty('contains')){
    Object.defineProperty(q[k], '$regex',
                Object.getOwnPropertyDescriptor(q[k], 'contains'));
    delete q[k]['contains'];
  }
}

console.log(q);
Mamun
  • 66,969
  • 9
  • 47
  • 59
1
 for(const obj of Object.values(q)) {
   obj.$regex = obj.contains;
   delete obj.contains;
 }

Just go over all values inside q and copy the contains property into the $regex property.

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
  • 2
    This code will fail in strict mode if `q` contains a non object property. The loop could be secured with `if (obj.hasOwnProperty("contains"))` – Guillaume Georges Jul 18 '18 at 14:17
0

var q = {
    email: {contains: "noname@hotmail.com"},
    name: {contains: "someuser"}
};
Object.keys(q).forEach(k => {

if (typeof q[k].contains != 'undefined'){
    q[k].$regex =  q[k].contains;
    delete q[k].contains;
}
})

console.log(q);

Other version using Es 6 features

const renameProp = (
  oldProp,
  newProp,
  { [oldProp]: old, ...others }
) => {
  return {
    [newProp]: old,
    ...others
  };
};
let q = {
    email: {contains: "noname@hotmail.com"},
    name: {contains: "someuser"}
};

let newObj = {}

for (let propName in q) {
newObj[propName] = renameProp("contains","$regex",q[propName])
}
console.log(newObj)
Vladu Ionut
  • 8,075
  • 1
  • 19
  • 30
0

To iterate over object keys first you have to fetch them, here is one simple approach

const keys = Object.keys(q); // ["email", "name"]

Now iterate over the array which we got and perform regex testing;

keys.forEach(key => {
    let value = q[key].contains;
    // create $regex and assign value 
    // remove .contains
})
mumair
  • 2,768
  • 30
  • 39
  • Not sure on the commented out sections. How would you create $regex and assign value remove .contains property? – Ali Celebi Jul 18 '18 at 14:41
0

var q = {
  email: {
    contains: "noname@hotmail.com"
  },
  name: {
    contains: "someuser"
  },
  asdf: "asdf"
};

Object.keys(q).forEach(function(item, index) {
  if (typeof q[item] == "object" && q[item].contains) {
    q[item].$regex = q[item].contains;
    delete q[item].contains;
  }
})
Vinod Sai
  • 1,956
  • 3
  • 13
  • 23
0

You can loop through the objects and first put current value of contains property in $regex and then delete the contains property.

Below is working code:

var q = {
  email: {
    contains: "noname@hotmail.com"
  },
  name: {
    contains: "someuser"
  }
};

for (var i of Object.values(q)) {
  if (i.hasOwnProperty("contains")) {
      i.$regex = i.contains;
      delete i.contains;
    }
  }



  console.log(q);
Vikasdeep Singh
  • 20,983
  • 15
  • 78
  • 104