Home > Net >  Creating new json by taking url addresses from existing json and split it up Javascript
Creating new json by taking url addresses from existing json and split it up Javascript

Time:10-18

I have a json file.

      [
  {
  "line": 1,
  "elements": [
    {
      "start_timestamp": "2022-10-17T20:07:41.706Z",
      "steps": [
        {
          "result": {
            "duration": 12216552000,
            "status": "passed"
          },
          "line": 5,
          "name": "m0e",
          "match": {
            "location": "seleniumgluecode.book.user_is_on_homepagee()"
          },
          "keyword": "Given "
        },
        {
          "result": {
            "duration": 2074982200,
            "status": "passed"
          },
          "line": 6,
          "name": "m1e1",
          "match": {
            "location": "seleniumgluecode.book.user_enters_Usernamee()"
          },
          "keyword": "When "
        }
      ],
      "tags": [
        {
          "name": "@Smokee"
        }
      ]
    },
    {
      "start_timestamp": "2022-10-17T20:08:12.284Z",
      "steps": [
        {
          "result": {
            "duration": 12090584100,
            "status": "passed"
          },
          "line": 12,
          "name": "m0e2",
          "match": {
            "location": "seleniumgluecode.book2.user_is_on_homepageee()"
          },
          "keyword": "Given "
        }
      ],
      "tags": [
        {
          "name": "@Smokee"
        }
      ]
    }
  ],
  "name": "Login Featuree",
  "description": "  Verify if user is able to Login in to the sitee",
  "id": "login-featuree",
  "keyword": "Feature",
  "uri": "file:src/test/java/features/tribe/squad1/kitab.feature",
  "tags": []
  },
  {
  "line": 1,
  "elements": [
    {
      "start_timestamp": "2022-10-17T20:08:34.480Z",
      "steps": [
        {
          "result": {
            "duration": 11366098500,
            "status": "passed"
          },
          "line": 5,
          "name": "m0e",
          "match": {
            "location": "seleniumgluecode.book.user_is_on_homepagee()"
          },
          "keyword": "Given "
        }
      ],
      "tags": [
        {
          "name": "@Smokee"
        }
      ]
    }
  ],
  "name": "Login Featureefghfgh",
  "description": "  Verify if user is able to Login in to the sitee",
  "id": "login-featureefghfgh",
  "keyword": "Feature",
  "uri": "file:src/test/java/features/tribe1/squad2/kitab2.feature",
  "tags": []
  },
  {
  "line": 19,
  "elements": [
    {
      "start_timestamp": "2022-10-17T20:09:40.836Z",
      "steps": [
        {
          "result": {
            "duration": 12761711100,
            "status": "passed"
          },
          "line": 23,
          "name": "m0e",
          "match": {
            "location": "seleniumgluecode.book.user_is_on_homepagee()"
          },
          "keyword": "Given "
        }
      ],
      "tags": [
        {
          "name": "@Smokee"
        }
      ]
    }
  ],
  "name": "X Feature",
  "description": "  Verify if user is able to Login in to the sitee",
  "id": "login-featuree",
  "keyword": "Feature",
  "uri": "file:src/test/java/features/tribe2/test.feature",
  "tags": []
  }
  ]

I am getting url addresses in this array

    document.addEventListener("DOMContentLoaded", () => {
    var i = report.length;
    var array = [];
    for(x = 0; x < i; x  ){
        array.push(report[x].uri.split("/"));

    }
    console.log(array2);
});

This return me :

  0:
  (7) ['file:src', 'test', 'java', 'features', 'tribe1', 'squad1', 'kitab.feature']
  1:
  (7) ['file:src', 'test', 'java', 'features', 'tribe1', 'squad2', 'kitab2.feature']
  2:
  (6) ['file:src', 'test', 'java', 'features', 'tribe2, kitab3.feature']

I don't need file:src, test, java, features. Deleting them in 3 arrays and getting a unique array like this:

  0:
  (3) ['tribe1', 'squad1', 'kitab.feature']
  1:
  (3) ['tribe1', 'squad2', 'kitab2.feature']
  2:
  (2) ['tribe2, kitab3.feature']

Finally, if there are 2 elements before the .feature, I need to create a new array by considering 1 as squad and 2 as tribe. Like this: Diagram

[tribe1
    squad1
        elem 
             1
             2
        name
        url
    squad2
        elem 
             1
             2
        name
        url
tribe2
    elem 
         1
         2
    name
    url
]

How can I do that?. Thank you.

CodePudding user response:

You should try destructing the array. An example is shown below:

[a,b,c,d, ...rest] = ['file:src', 'test', 'java', 'features', 'tribe1', 'squad1', 'kitab.feature']
console.log(rest);

CodePudding user response:

You can transform or create a new array based on input array with this simple logic with the help of Array.map() along with String.split() and Array.splice() method.

Live Demo :

const arr = [
  {
    "line": 1,
    "uri": "file:src/test/java/features/tribe/squad1/kitab.feature"
  },
  {
    "line": 1,
    "uri": "file:src/test/java/features/tribe1/squad2/kitab2.feature"
  },
  {
    "line": 19,
    "uri": "file:src/test/java/features/tribe2/test.feature"
  }
];

const res = arr.map(({ uri}) => uri.split('/').splice(4));

console.log(res);

  • Related