Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 140071

Regex for substring on NodeJS using pinch.js

$
0
0

I am modifying JSON files/Javascript object using Pinch library: https://github.com/Baggz/Pinch

In this example, pinch() with regex parameter /id/ modifies ALL the id values to 321.

What I want to do is be able to change the value of all ids but only for a specific "Requestor", based on a parameter (requestorToChange). Let's say "RequestorX". How do I write the regex for it?

var sample = {
  "RequestorX":
  [{
    user: {
      id: '123'
    },
    request: {
      id: '456'
    },
    book: {
      id: '789'
    }
  }],
  "RequestorY":
  [{
    user: {
      id: '111'
    },
    request: {
      id: '222'
    },
    book: {
      id: '333'
    }
  }]
};

const requestorToChange = 'RequestorX'

pinch(sample, /id/, function(path, key, value) {
  return '321';
});

console.log(JSON.stringify(sample))

I know that one option is to just do:

pinch(sample['RequestorX'], /id/, function(path, key, value) {
  return '321';
});

But I need to be able to do it via the regex field since in reality, I will be manipulating deeply nested JSON files.


Viewing all articles
Browse latest Browse all 140071

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>