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
I add code below.
One way would be to loop thru the array using
array.some
to check if the key exists. Here is an example: