Home > Mobile >  How to add title/key to an array of multiple objects in javascript?
How to add title/key to an array of multiple objects in javascript?

Time:11-13

I have the following function, where its extracting some data from some files and return in an array

const extractTestCases = () => {
    const filesArray = []
    const filterTestSuite = files.filter(test => test.includes('.test.ts'))
    filterTestSuite.forEach(testsuite => {
        const feature = regexMatcher(testsuite, featureRegex, 1)
        const testSuites = fs.readFileSync(testsuite, { encoding: "utf8" });
        const testCases = regexMatcher(testSuites, TestRegex, 1)
        filesArray.push({ [feature]: testCases })
    })
    return filesArray;
}

which gives me an output like the following

[
  {
    featureCustom: [ 'somethig' ]
  },
  {
    featureInsp: [
      'bla bla',
      'foo foo'
    ]
  }
]

how to make it generate an output like the following

[
    {
        "features": [
            {
                "featureCustom": [ 'somethig'
                ]
            },
            {
                "featureInsp": ['bla bla','foo foo'
                ]
            }
        ]
    }
]

CodePudding user response:

Something like?...

const extractTestCases = () => {
    const filesArray = []
        filesArray.push({ [feature]: testCases })
    return [
      {
        features: filesArray
      }
    ];
}
  • Related