0

I want to add an object in my json, but to do that I need to select the correct team 'servX' by his ID and after I need to select a member by his name to add the element inside his planning. The team id and name are var. You will be able to see my json file and my code after. Thank you !

JSON

[
  {
    "name": "serv1",
    "id": "1",
    "members": [
      {
        "id": "1",
        "name": "Michel",
        "planning": [
          {
            "id": "26/07/2022",
            "type": "TT"
          },
          {
            "id": "16/08/2022",
            "type": "TT"
          }
        ]
      },
      {
        "id": "2",
        "name": "Paul",
        "planning": [
          {
            "id": "25/07/2022",
            "type": "TT"
          },
          {
            "id": "28/07/2022",
            "type": "TT"
          }
        ]
      }
    ]
  },
  {
    "name": "serv2",
    "id": "2",
    "members": [
      {
        "id": "1",
        "name": "Michel",
        "planning": [
          {
            "id": "16/07/2022",
            "type": "TT"
          }
        ]
      },
      {
        "id": "2",
        "name": "Paul",
        "planning": [
          {
            "id": "18/07/2022",
            "type": "TT"
          }
        ]
      }
    ]
  }
]

Server.js

app.get("/add", (req, res) => {
  let id = req.query.id;
  id = id.substr(0, 10);
  let type = req.query.type;
  let team = req.query.team;
  let name = req.query.name;
  if (type == "orange") {
    type = "none";
  } else if (type == "green") {
    type = "TT";
  }
  let toPush = {
    id: id,
    type: type,
  };
  (team = parseFloat(team)), (team -= 1);
  fs.readFile(__dirname + "/public/services.json", (err, data) => {
    if (err) throw err;
    let json = JSON.parse(data);
  });
  • Does this answer your question? [Get JavaScript object from array of objects by value of property](https://stackoverflow.com/questions/13964155/get-javascript-object-from-array-of-objects-by-value-of-property) – derpirscher Jul 26 '22 at 09:31
  • Or alternatively https://stackoverflow.com/questions/7364150/find-object-by-id-in-an-array-of-javascript-objects – derpirscher Jul 26 '22 at 09:31

0 Answers0