I would like to be able to replace certain keys within an object of dynamic size using Javascript. For example, I have an object that might look like this:
var obj = {
where: {
hypes: {
lte: 12 <-- replace key
},
or: [
{
hypes: {
lt: 14 <-- replace key
}
},
{
id: {
lt: 10 <-- replace key
}
}
]
}
}
In order for this object to be used with Sequelize I need to convert the following keys to Sequelize operators: lt, or, and lte. I've created an operators object that acts as a dictionary so I can pull the Sequelize operator from it:
const operators = {
'eq': [Op.eq],
'lte': [Op.lte],
'lt': [Op.lt],
'or': [Op.or]
};
I was thinking I could use the operators object to match the key of the obj and replace it with the value of the operators object. I figured I could use the following recursive function to loop through the obj given its unknown size:
const iterate = (obj) => {
Object.keys(obj).forEach(key => {
console.log(`key: ${key}, value: ${obj[key]}`)
if (typeof obj[key] === 'object') {
iterate(obj[key])
}
})
}
The above function will check if the value of a key is an object and will call the iterate() method again.
Thus I can call: iterate(obj) and it'll recursively map through all nested properties in the object.
The issue I am having is how do I replace the keys when they match a property inside the operators object so that I'll end up with an obj that looks like this:
var obj = {
where: {
hypes: {
lte: 12
},
[Op.or]: [ <-- changed
{
hypes: {
[Op.lt]: 14 <-- changed
}
},
{
id: {
[Op.lt]: 10 <-- changed
}
}
]
}
}
Thanks!