avatar
find object in arrays of nested objects Javascript

• Assume that we have arrays of nested objects as below:

[
  {
    "_index": "flagtick_index",
    "_type": "_doc",
    "_id": "106",
    "_score": 7.3813467,
    "_source": {
      "index": {
        "_index": "flagtick_index",
        "_id": 107
      },
      "body": {
        "id": 107,
        "name": "build AEM bundle and package",
        "slug": "build-aem-bundle-and-package",
        "keywords": [
          "build AEM bundle and package"
        ],
        "user_id": 1,
        "aspect_id": 23,
        "user": {
          "id": 1,
          "name": "Vuong Nguyen",
          "avatar": "https://flagtick.com/ygclpcatla1mns1deuo3vh0w.jpg"
        },
        "aspect": {
          "id": 23,
          "name": "AEM"
        },
        "tag": [
          {
            "id": 750,
            "snippet_id": 107,
            "tag_id": 84,
            "tag": {
              "id": 84,
              "name": "AEM",
              "slug": "aem"
            }
          }
        ],
        "description": "> Build on Author Instancemvn ",
        "relative": []
      }
    }
  },
  {
    "_index": "flagtick_index",
    "_type": "_doc",
    "_id": "116",
    "_score": 6.386127,
    "_source": {
      "index": {
        "_index": "flagtick_index",
        "_id": 117
      },
      "body": {
        "id": 117,
        "name": "build and install single package on AEM instance",
        "slug": "build-and-install-single-package-on-aem-instance",
        "keywords": [
          "build and install single package on AEM instance"
        ],
        "user_id": 1,
        "aspect_id": 23,
        "user": {
          "id": 1,
          "name": "Vuong Nguyen",
          "avatar": "https://flagtick.com/ygclpcatla1mns1deuo3vh0w.jpg"
        },
        "aspect": {
          "id": 23,
          "name": "AEM"
        },
        "tag": [
          {
            "id": 393,
            "snippet_id": 117,
            "tag_id": 84,
            "tag": {
              "id": 84,
              "name": "AEM",
              "slug": "aem"
            }
          }
        ],
        "description": "mvn clean install",
        "relative": [
          "skip unit tests written in java code during build",
          "build AEM bundle and package",
          "build single package"
        ]
      }
    }
  }
]

• In the next step, we will use includes or the === operator to check for the existence of an item in the array.

function isObjInArrayWithSingle(temps, targetSlug) {
  return temps.some((temp) => temp._source.body.slug === targetSlug);
}

or

function isObjInArrayWithMultiple(temps, targetSlug) {
  return temps.some((temp) =>
    temp._source.body.relative.includes(targetSlug)
  );
}

• On the other hand, we will find the object using the .find() method and apply conditions as mentioned above, using includes or the === operator.

function findObjBySlugInArrayWithMultiple(temps, targetSlug) {
  return temps.find((temp) =>
    temp._source.body.relative.includes(targetSlug)
  );
}
24
check if element exists in array using includes method check if any string would be existed within given string
You need to login to do this manipulation!