0

I want to pass an array into an hidden input and be able to return it, but instead of doing this, the value is a string, as shown below :

Input :

<input type="hidden" name="LabelProduitInput" value=${labelProduitsArray}>

retrieve hidden input value and log it :

const labelProduitsArray = [product.fields.LabelProduitInput];

console.log(labelProduitsArray);

Result :

[ "BlaBla,BloBlo" ]

Now, i should split this string to have the result i want like so:

["BlaBla","BloBlo"]

Is there a way to directly return an array ?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • Yes, attribute values are always strings. What do you mean by “return it”? Return where? What are you trying to do with this array? How is TypeScript involved? – Sebastian Simon Dec 10 '22 at 08:15
  • `product.fields.LabelProduitInput.split(",")` ... Just split the string – cloned Dec 10 '22 at 08:16
  • @cloned If the Array should be stringified at all, then JSON should be used. Splitting by a comma is not the correct approach. – Sebastian Simon Dec 10 '22 at 08:19
  • @SebastianSimon Ok, i want to extract this array values in my database, so i do this server side, this is why i retrieve my array with the statement product.fields.Nameoftheinput in a route function with typescript –  Dec 10 '22 at 08:19
  • So you want to send and receive it in a request, not return it. Why not just use AJAX using JSON or FormData instead of the default action of a `
    `? See [`JSON.stringify`](//developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify), [`JSON.parse`](//developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse), [`fetch`](//developer.mozilla.org/en/docs/Web/API/fetch), [`FormData`](//developer.mozilla.org/en/docs/Web/API/FormData).
    – Sebastian Simon Dec 10 '22 at 08:23
  • @cloned it works, thanks, this is the result i want –  Dec 10 '22 at 08:26
  • @SebastianSimon Yes i wanted to do this, now it works just splitting the array –  Dec 10 '22 at 08:31
  • Please don't add things like "solved" to a question title. – Mark Rotteveel Dec 11 '22 at 09:33

0 Answers0