How to insert array elements into an array?

I try so but fail:

var t = "'-pro', 'co', 'optimize', 't2'"; [eval(t), output, input] 
  • eval executes the comma operator , returns the last element. Do you need to insert a string of this format? - user207618
  • 2
    [...eval(`[${t}]`), output, input] - Grundy
  • one
    @Other stern unfeeling people who simply answer the question posed without ceremony and do not think about the consequences) - andreymal
  • one
    @Grundy, well, this principle saved me more than once :) - user207618
  • one
    @YurySvetlov, in any case, check what has come. And yes - spread is pretty new, so look for compatibility. - user207618

1 answer 1

Single quotes to process the place a file.

 var output = "out"; var input = "in"; var t = "'-pro', 'co', 'optimize', 't2'"; var arr = t.split(", "); arr.push(output); arr.push(input); console.log(arr); // извращение без метода push var arr1 = eval("[" + t + ", '" + output + "', '" + input + "']"); console.log(arr1); 

  • Thanks, but you need without the push method. That is, the push inserts data into the array, and it is necessary to insert the array element into the array. Roughly speaking, there is an array and you probably need to add a string to it in the form of an array, which you need to somehow convert into an object — something like that. - Yuri Svetlov
  • What a perversion? Array#concat is :) - user207618
  • Quotes can be cut off with a file: arr.map(e => e.replace(/^['"]|['"]$/g, '');) . - user207618