0

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",
]
Flashman
  • 33
  • 5
  • Does this answer your question? [Simplest code for array intersection in javascript](https://stackoverflow.com/questions/1885557/simplest-code-for-array-intersection-in-javascript) – Andy Ray Sep 28 '22 at 23:24
  • first thing you'll need to do is parse that JSON into an object, then it'll be simpler – Jaromanda X Sep 29 '22 at 00:05
  • judging from the identical comment you've made to the 3 answers that actually DO work, your actual data is probably not what you say it is in the question – Jaromanda X Sep 29 '22 at 00:07
  • in my JSON when i try to use ['cm:taggable'] console show me TypeError: Cannot read properties of undefined (reading 'cm:taggable') but if i remove ['cm:taggable'] console show me all the properties working well with his value ["cm:title"],["cm:taggable"],["cm:description"] but i only need ['cm:taggable'] this happen when i try to get only one property like [cm:taggable] or [cm:title] – Flashman Sep 29 '22 at 01:21

4 Answers4

0

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))
BehRouz
  • 1,209
  • 1
  • 8
  • 17
  • when i try to use ['cm:taggable'] console show me TypeError: Cannot read properties of undefined (reading 'cm:taggable') but if i remove ['cm:taggable'] console show me all the properties working well with his value ["cm:title"],["cm:taggable"],["cm:description"] but i only need ['cm:taggable'] this happen when i try to get only one property like [cm:taggable] or [cm:title] – Flashman Sep 28 '22 at 23:41
0

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`)
})
Jairo Py
  • 615
  • 6
  • 12
  • when i try to use ['cm:taggable'] console show me TypeError: Cannot read properties of undefined (reading 'cm:taggable') but if i remove ['cm:taggable'] console show me all the properties working well with his value ["cm:title"],["cm:taggable"],["cm:description"] but i only need ['cm:taggable'] this happen when i try to get only one property like [cm:taggable] or [cm:title] – Flashman Sep 28 '22 at 23:55
0

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
PeterKA
  • 24,158
  • 5
  • 26
  • 48
  • in my JSON when i try to use ['cm:taggable'] console show me TypeError: Cannot read properties of undefined (reading 'cm:taggable') but if i remove ['cm:taggable'] console show me all the properties working well with his value ["cm:title"],["cm:taggable"],["cm:description"] but i only need ['cm:taggable'] this happen when i try to get only one property like [cm:taggable] or [cm:title] – Flashman Sep 28 '22 at 23:57
  • You would need to give the full path of the property whose value you need; `obj.entry.properties["cm:taggable"]` or `obj["entry"]["properties"]["cm:taggable"]`. However, that does not seem to be the error you're getting; it sounds to me like `obj` is undefined. Perhaps you're trying to access the properties before there's a response from the server. **Please show a short snippet of your code indicating both where the data is being fetched and where you're trying to get the value of `cm:taggable`**, in your question, as an update. – PeterKA Sep 29 '22 at 12:29
0

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);
Debug Diva
  • 26,058
  • 13
  • 70
  • 123
  • when i try to get properties['cm:taggable'] console show me a error TypeError: Cannot read properties of undefined (reading 'cm:taggable') – Flashman Sep 29 '22 at 08:34
  • @Flashman That means `properties` key is not available, Looks like you don't have the same response object which you posted in a post. Can you please check your JSON. – Debug Diva Sep 29 '22 at 09:33