hello how can i compare two arrays one from my data json and the second from a array ?
i need to know if the ids of "cm:taggable" exist in my secondArray
JSON
{
"entry": {
"isFile": true,
"createdByUser": {
"id": "admin",
"displayName": "Administrator"
},
"modifiedAt": "2022-03-09T15:57:45.470+0000",
"nodeType": "cm:content",
"content": {
"mimeType": "application/zip",
"mimeTypeName": "ZIP",
"sizeInBytes": 509390,
"encoding": "UTF-8"
},
"parentId": "10o39gc76-8765-9f6b-8766904j55",
"createdAt": "2022-03-04T19:44:47.009+0000",
"isFolder": false,
"modifiedByUser": {
"id": "Prov1",
"displayName": "Prov1"
},
"name": "bookOne.zip",
"id": "ct73849o983-i383ui-6tre-w9e0-2h2f2k3i846738",
"properties": {
"cm:title": "bookOne",
"cm:taggable": [
"6814d5c5-9c56-428f-a4ac-aa13ef6b1ef8",
"964d2c90-62e5-448d-a062-5e5297e518d9",
"b87f6b9e-edbe-49d1-8a7c-05a35fe0eb78"
],
"cm:description": "Book One"
}
}
}
second array
const scondArray = [
"6814d5c5-9c56-428f-a4ac-aa13ef6b1ef8",
"343434-9c56-428f-a4ac-193494f12948",
"61231-9c56-428f-a4ac-i18838223344",
]
4
Answers
You need to select the array which is nested in the JSON variable and write a function to do the comparison.
You just need to iterate trough the cm:tagable array values, and compare them against the secondArray value, like this:
You can use
Array#filter
to get only thoseids
infirstArray
that are also insecondArray
; otherwise the test (boolean
) for each of first arrayid
is:You can achieve that with a single line of code by just using
Array.some()
method. It returns true if, in the array, it finds an element for which the provided function returns true; otherwise it returns false.Live Demo :