skip to Main Content

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


  1. You need to select the array which is nested in the JSON variable and write a function to do the comparison.

    const jsonData = {
        "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"
            }
        }
    }
    
    const firstArr = [
      "6814d5c5-9c56-428f-a4ac-aa13ef6b1ef8",
      "343434-9c56-428f-a4ac-193494f12948",
      "61231-9c56-428f-a4ac-i18838223344",
    ]
    
    const secondArr = [
      "6814d5c5-9c56-428f-a4ac-aa13ef6b1ef8",
      "964d2c90-62e5-448d-a062-5e5297e518d9",
      "b87f6b9e-edbe-49d1-8a7c-05a35fe0eb78"
    ]
    
    const compare = (arr1, arr2) => arr1.length == arr2.length && arr1.reduce((a, b) => a && arr2.includes(b), true)
    
    const toCompare = jsonData["entry"]["properties"]["cm:taggable"]
    
    console.log('firstArr', compare(firstArr, toCompare))
    console.log('secondArr', compare(secondArr, toCompare))
    Login or Signup to reply.
  2. You just need to iterate trough the cm:tagable array values, and compare them against the secondArray value, like this:

    const Myjson = {
        "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"
            }
        }
    }
    
    const ids = Myjson.entry.properties['cm:taggable'];
    const scondArray = [
    "6814d5c5-9c56-428f-a4ac-aa13ef6b1ef8",
    "343434-9c56-428f-a4ac-193494f12948",
    "61231-9c56-428f-a4ac-i18838223344",
    ];
    
    ids.forEach((id, index) => {
      id === scondArray[index] 
      ? console.log(`the id ${id} exists in second array`)
      : console.log(`the id ${id} doesnt exist in second array`)
    })
    Login or Signup to reply.
  3. You can use Array#filter to get only those ids in firstArray that are also in secondArray; otherwise the test (boolean) for each of first array id is:

    secondArray.includes( id )
    
    const obj = { "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" } } },
    
    firstArray = obj.entry.properties["cm:taggable"],
    
    secondArray = [
    "6814d5c5-9c56-428f-a4ac-aa13ef6b1ef8",
    "343434-9c56-428f-a4ac-193494f12948",
    "61231-9c56-428f-a4ac-i18838223344",
    ],
    
    output = firstArray.filter(id => secondArray.includes( id ));
    
    console.log( output );
    //OUTPUT: ["6814d5c5-9c56-428f-a4ac-aa13ef6b1ef8"]
    //The only id in firstArray that's also in secondArray
    Login or Signup to reply.
  4. 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 :

    const jsonObj = {
      "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"
        }
      }
    };
    
    const secondArray = [
    "6814d5c5-9c56-428f-a4ac-aa13ef6b1ef8",
    "343434-9c56-428f-a4ac-193494f12948",
    "61231-9c56-428f-a4ac-i18838223344",
    ]
    
    const isIDExist = jsonObj.entry.properties['cm:taggable'].some(item => secondArray.includes(item));
    
    console.log(isIDExist);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search