How split each item inside an Array String

Viewed 27

how can i do a split for each item inside an array of strings ?

i am trying with for, but the typescript acuses Type 'string[]' is not assignable to type 'string'.. I don't know another way to made that.

to contextualize , I am converting the request.files into a JSON , and JSON to Array, and i need the first numbers of the file name , which will be used as an ID to the database

my try with for:

let ids: Array<string> = []; //Array declaration
var myfiles = JSON.parse(JSON.stringify(req.files)) // Convert Request Files to JSON

myfiles.map((item: any) => {
            ids.push(item.filename) //Push each file name into the array
})

for(var i = 0; i< ids.length; i++){
            ids[i] = ids[i].split('_',1) // Here would change the name of each item inside the array, but it accuses the aforementioned error
}

and my try with foreach, who accuses the same error

    ids.forEach((item, index) => {
        ids[index] = item.split('_', 1)
    })

SOLUTION

as our friend long_hair_programmer suggested, changing ids[i] = ids[i].split('_',1) to ids[i] = ids[i].split('_',1)[0] resolves the problem

0 Answers
Related