3

This is an object to be processed:

var q = {
    email: {contains: "[email protected]"},
    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.

0

6 Answers 6

2

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

You can try the following way:

var q = {
    email: {contains: "[email protected]"},
    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);

Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

This code will fail in strict mode if q contains a non object property. The loop could be secured with if (obj.hasOwnProperty("contains"))
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
})

1 Comment

Not sure on the commented out sections. How would you create $regex and assign value remove .contains property?
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: "[email protected]"
  },
  name: {
    contains: "someuser"
  }
};

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



  console.log(q);

Comments

0

var q = {
    email: {contains: "[email protected]"},
    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: "[email protected]"},
    name: {contains: "someuser"}
};

let newObj = {}

for (let propName in q) {
newObj[propName] = renameProp("contains","$regex",q[propName])
}
console.log(newObj)

Comments

0

var q = {
  email: {
    contains: "[email protected]"
  },
  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;
  }
})

2 Comments

How exactly do you apply regex here on q[item].contains?
Updated my answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.