0

I have JSON with 10000 unique records and I need to add another field with a unique value to every record. How do I add that?

[
{
_id: "5fffd08e62575323d40fca6f",
wardName: "CIC",
region: "ABC",
location: "AAA",
specialism: "CSR",
__v: 0
},
.
.
.
]

The JSON is stored in variable showWard. How do I add an action field in my JSON with value = './wardName' where wardName is already a field in my JSON? This is my current code:

app.get('/wards', function (req, res) {
    Ward.find({}, function (err, showWard) {
        if (err) { console.log(err); return; }
        return res.send(showWard);
    });
});
  • That's an array of objects and not [JSON](https://www.json.org/json-en.html) -> [What is the difference between JSON and Object Literal Notation?](https://stackoverflow.com/questions/2904131/what-is-the-difference-between-json-and-object-literal-notation) – Andreas Jan 19 '21 at 08:36

1 Answers1

0

Using a .map()? I don't know the logic for determinate the gender, but in Ward.find() callback you can add a thing like that:

app.get('/wards', function (req, res) {
    Ward.find({}, function (err, showWard) {
        if (err) { console.log(err); return; }

        const newShowWard = showWard.map(ward => {
            ward.gender = "BOH"; 
            return ward;
        })
        
        return res.send(newShowWard);
    });
});
AlTheLazyMonkey
  • 1,190
  • 1
  • 8
  • 20