Home > Software design >  How to make an array of objects from dynamic values?
How to make an array of objects from dynamic values?

Time:11-12

I have the following function

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

filterFile is an array for multiple files where I'm making forEach loop to extract some information from each file, what I want to do is to return an array of objects like this

[{"name of the file (testSuite)":["extracted value from this file regexMatcher(testSuites, TestRegex, 1)"]},{"testSuite2":["","",""]},...]

CodePudding user response:

Try something like this :

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