You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, I want to replace all occurrences of the array or object. How to do this?
example::
var dot = require('dot-object');
var str_replacement = 'XXXX';
let inputData = {
order_status: 1,
orders: [
{
order_id: 'A001',
date: "10-10-2019"
},
{
order_id: 'A002',
date: "10-11-2019"
}
],
shipping: {
address: "9th main, 6th cross, jp nagar"
},
billing: {
name: "John",
address: "10th floor, abc road."
}
}
dot.str('order_status', str_replacement, inputData); // works fine
dot.str('shipping.address', str_replacement, inputData); // works fine
dot.str('orders[*].order_id', str_replacement, inputData); // does not work (I want to replace all order_id of the array with the given replacement value)
dot.str('billing.*', str_replacement, inputData); // does not work (I want to replace all child values of the object with the given replacement value)
console.log(JSON.stringify(inputData, null, 2));
Expected Result
{
"order_status": "XXXX",
"orders": [
{
"order_id": "XXXX",
"date": "10-10-2019"
},
{
"order_id": "XXXX",
"date": "10-11-2019"
}
],
"shipping": {
"address": "XXXX"
},
"billing": {
"name": "XXXX",
"address": "XXXX"
}
}
The text was updated successfully, but these errors were encountered:
I know this is a semi-old question but I recently ran into a similar issue and just wanted to share my workaround, in case anyone else comes across this.
After spending a little time trying to figure out how I could extend the package to do this, instead I ultimately came up with something like this:
// Use the `.dot()` method to flatten the object into its dot-string keys.constdotted=dot.dot(inputData);// Filter those keys to find the ones that match some pattern and iterate.Object.keys(dotted).filter((k)=>k.includes('.order_id')).forEach((k)=>{// Finally, do any processing or replacement.constoriginalValue=dot.pick(k,inputData);constnewValue=originalValue.split('').map(v=>'X').join('');dot.str(k,newValue,inputData);});
Hi, I want to replace all occurrences of the array or object. How to do this?
example::
The text was updated successfully, but these errors were encountered: