Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Matching all elements of the given Array. #47

Open
shahidmau opened this issue Jan 22, 2020 · 1 comment
Open

Matching all elements of the given Array. #47

shahidmau opened this issue Jan 22, 2020 · 1 comment

Comments

@shahidmau
Copy link

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"
    } 
  } 
@velojason
Copy link

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.
const dotted = 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.
  const originalValue = dot.pick(k, inputData);
  const newValue = originalValue.split('').map(v => 'X').join('');
  dot.str(k, newValue, inputData);
});

Results in:

{
  ...
  orders: [
    { order_id: 'XXXX', date: '10-10-2019' },
    { order_id: 'XXXX', date: '10-11-2019' }
  ]
}

Hope this helps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants