skip to Main Content

I need a way to return true or false if in changeFields array "printLogs" key is present or not. The object is shown below. I have tried looping but somehow cant get my head to wrap around it.

const printObj = [
  {
    _id: "656a69e14",
    document: { _id: "65b891" },
    user: "654cfbba3ea455b55b057195",
    changeFields: [
      {
        change: "update",
        quantity: [
          2,
          3,
          "2023 1 Oz Silver Maple Leaf Coin - Royal Canadian Mint",
        ],
      },
      { change: "update", subTotal: ["77.88", "116.82"] },
      { change: "update", totalAmount: ["107.88", "146.82"] },
    ],
    createdAt: "2023-12-01T19:10:54.575Z",
    updatedAt: "2023-12-01T19:10:54.575Z",
    __v: 0,
  },
  {
    _id: "65e0e",
    document: { _id: "651" },
    user: "",
    changeFields: [{ printLogs: "An order has been printed" }],
    createdAt: "",
    updatedAt: "",
    __v: 0,
  },
{},

]

I have tried javascript in built functions. The below worked to some degree but I need to return true or false if printLogs is present in changeFields.

const value = printObj.filter(function (x) {
  x.changeFields.filter(function (o) {
    console.log(o, o.hasOwnProperty("printLogs"));
  });
});

2

Answers


  1. I add code below.

    const printObj = [
      {
        _id: "656a69e14",
        document: { _id: "65b891" },
        user: "654cfbba3ea455b55b057195",
        changeFields: [
          {
            change: "update",
            quantity: [
              2,
              3,
              "2023 1 Oz Silver Maple Leaf Coin - Royal Canadian Mint",
            ],
          },
          { change: "update", subTotal: ["77.88", "116.82"] },
          { change: "update", totalAmount: ["107.88", "146.82"] },
        ],
        createdAt: "2023-12-01T19:10:54.575Z",
        updatedAt: "2023-12-01T19:10:54.575Z",
        __v: 0,
      },
      {
        _id: "65e0e",
        document: { _id: "651" },
        user: "",
        changeFields: [{ printLogs: "An order has been printed" }],
        createdAt: "",
        updatedAt: "",
        __v: 0,
      },
      {},
    ];
    
    const result = printObj.map(
      (obj) =>
        !!obj.changeFields?.filter(
          (item) => Object.keys(item).indexOf("printLogs") !== -1
        ).length
    );
    
    console.log(result);
    Login or Signup to reply.
  2. One way would be to loop thru the array using array.some to check if the key exists. Here is an example:

    const printObj = [
      {
        _id: "656a69e14",
        document: { _id: "65b891" },
        user: "654cfbba3ea455b55b057195",
        changeFields: [
          {
            change: "update",
            quantity: [
              2,
              3,
              "2023 1 Oz Silver Maple Leaf Coin - Royal Canadian Mint",
            ],
          },
          { change: "update", subTotal: ["77.88", "116.82"] },
          { change: "update", totalAmount: ["107.88", "146.82"] },
        ],
        createdAt: "2023-12-01T19:10:54.575Z",
        updatedAt: "2023-12-01T19:10:54.575Z",
        __v: 0,
      },
      {
        _id: "65e0e",
        document: { _id: "651" },
        user: "",
        changeFields: [{ printLogs: "An order has been printed" }],
        createdAt: "",
        updatedAt: "",
        __v: 0,
      },
      {},
    ];
    
    const result = printObj.some((o) => o.changeFields?.some((f) => f.printLogs) ?? false)
    
    console.log(result)
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search