Transform String into an Array – Javascript

Posted on

Question :

With this return I’m getting the following string :

return $data.requestSuprimentos.value; 

String

[[1, X340H22G, 30000, 3], [2, 64418XL, 32000, 1], [3, X644X11L, 32000, 1], [4, 64018HL, 21000, 1], [5, 12A7610, 32000, 1], [6, SX340H11G, 6000, 2], [7, C930X72G, 53000, 3], [8, C930X73G, 53000, 3], [9, SE352H11L, 9000, 2], [10, Q2612A, 2000, 1], [11, Q7553A, 3000, 1], [12, Q7553X, 7000, 1], [13, C3903A, 4000, 1], [14, C4092A, 2500, 1], [15, Q2613X, 4000, 1], [16, C5240MH, 5000, 5], [17, C5340CX, 7000, 5], [18, C5240KH, 8000, 1], [19, C5340YX, 7000, 5], [20, 12A3160, 20000, 1], [21, 12A7415, 10000, 1], [22, Q5949A, 2500, 1], [23, Q5949X, 6000, 1], [24, 12B0090, 30000, 1], [25, C1823T, 1380, 14], [26, C4810A, 16000, 7], [27, C4844A, 1430, 12], [28, 12A8400, 6000, 1], [29, C4096A, 5000, 1], [30, C7115X, 3500, 1], [31, Q2610A, 6000, 1], [32, C4127X, 10000, 1], [33, 13T0101, 6000, 1], [34, 34018HL, 6000, 1], [35, SE250X22G, 30000, 4], [36, 12A4715, 12000, 1], [37, 2447B003AA, 48000, 1], [38, 1390A003AA, 90000, 1], [39, Q6511A, 6000, 1], [40, C4838A, 580, 14], [41, C4837A, 580, 14], [42, C4836A, 580, 14], [43, C6650FL, 1740, 1], [44, C6578A, 790, 14], [45, C5340MX, 7000, 5], [46, C930H2CG, 24000, 5], [47, C930H2YG, 24000, 5], [48, C930H2KG, 48000, 1], [49, C930H2MG, 24000, 5], [50, X945X2MG, 22000, 5], [51, X945X2YG, 22000, 5], [52, X945X2CG, 22000, 5], [53, X945X2KG, 36000, 1], [54, C5220KS, 3000, 1], [55, TN670, 7500, 1], [56, 113R00635, 24000, 1], [57, C8061X, 10000, 1], [58, TN560, 6500, 1], [59, Q3964A, 30000, 3], [60, 12A6160, 30000, 1], [61, TN570, 6700, 1], [62, TN460, 6000, 1], [63, C4813A, 24000, 7], [64, 2450B003AA, 9500, 5], [65, 2448B003AA, 9500, 5], [66, 2449B003AA, 9500, 5], [67, 12A7400, 3000, 1], [68, C53034X, 20000, 3], [69, 18S0090, 3200, 1], [70, Q2670A, 6000, 1], [71, Q2681A, 6000, 5], [72, Q2682A, 6000, 5], [73, Q2683A, 6000, 5], [74, Q3963A, 4000, 5], [75, E260A11L, 3500, 1], [76, SE450H11L, 11000, 2], [77, C4811A, 24000, 7], [78, C4812A, 24000, 7], [79, Q6511X, 12000, 1], [80, C6578D, 395, 14], [81, SML-D2850B, 5000, 1], [82, 6707A001AC, 9000, 8], [84, C5240CH, 5000, 5], [85, 0387B003AA, 47000, 1], [86, 12A8302, 30000, 3], [87, C5220CS, 3000, 5], [88, C5220YS, 3000, 5], [89, Q3960A, 5000, 1], [90, Q3961A, 4000, 5], [91, Q3962A, 4000, 5], [92, MLT-D209L, 5000, 1], [93, 1007B001AA, 15000, 8], [94, C5220MS, 3000, 5], [95, SCX-D4200A, 3000, 12], [96, C5240YH, 8000, 5], [98, X203A11G, 2500, 1], [99, X463X11G, 15000, 1], [100, Q6000A, 2500, 1], [101, Q6001A, 2000, 5], [102, Q6002A, 2000, 5], [103, Q6003A, 2000, 5], [104, C930X76G, 30000, 9], [105, SX203A11G-6K, 6000, 2], [106, 0250A002ADST, 6000, 8], [107, CARTUCHO HP TESTE, 6000, 12], [108, E460X11L, 15000, 1], [109, T654X11L, 36000, 1], [110, X654X11L, 36000, 1], [111, C544X1KG, 6000, 1], [113, C540X75G, 3600, 9], [114, 0251A001AA, 15000, 8], [115, C540H1YG, 2000, 5], [116, C544X1CG, 4000, 5], [117, C544X1MG, 4000, 5], [118, C544X1YG, 4000, 5], [119, 25A0013, 15000, 8], [120, X203H22G, 25000, 3], [121, MLT-D209L 3K, 3000, 1], [122, T650H11L, 25000, 1], [123, SE260X22G, 30000, 4], [124, C1823DL, 690, 14], [125, Q2613A, 2500, 1], [126, C540X74G, 30000, 3], [127, 51645A, 870, 12], [128, E450A11L, 6000, 1], [129, C736H1KG, 12000, 1], [130, C736H1YG, 10000, 5], [131, C736H1MG, 10000, 5], [132, C736H1CG, 10000, 5], [133, C734X24G, 20000, 3], [134, T654X80G, 36000, 1], [135, W84020H, 30000, 1], [136, C746H1KG, 10000, 5], [137, C748H1CG, 10000, 5], [138, C748H1MG, 10000, 5], [139, C748H1YG, 10000, 5], [140, C734X77G, 25000, 9], [141, SX264H11G, 9000, 2], [142, C734A1KG, 8000, 1], [143, 64480XW, 32000, 1], [144, C52025X, 35000, 9], [145, X748H1MG, 10000, 5], [146, X748H1YG, 10000, 5], [147, 51645GL, 415, 12], [148, 70C8XC0, 4000, 5], [149, 70C8XK0, 8000, 1], [150, 70C8XM0, 4000, 5], [151, 70C8XY0, 4000, 5], [152, X746H1KG, 12000, 12], [153, 52D4H00, 25000, 1], [154, 60F4X00, 20000, 1], [155, 50F0Z00, 60000, 3], [156, 50F4U00, 20000, 1], [157, 50F4X00, 10000, 1], [158, 12A6835, 20000, 1], [159, X748H1CG, 10000, 5], [160, C782X1KG, 15000, 1], [161, 12A5845, 25000, 1], [162, LS64X, 32000, 1], [163, 60FBX00, 20000, 1], [164, 50FBX00, 10000, 1], [165, T654X11B, 36000, 1], [166, X654X11B, 36000, 1], [167, C9381A, 16000, 7], [168, C9382A, 16000, 7], [169, E460X11B, 15000, 1], [170, 64418XB, 32000, 1], [171, 50FBU00, 20000, 1], [172, X463X11B, 15000, 1], [173, 12A7465, 32000, 1], [174, SE352H11B, 9000, 2], [175, 64018HB, 21000, 1], [176, CN053AL, 1000, 12], [177, CN054AL, 825, 14], [178, CN055AL, 825, 14], [179, CN056AL, 825, 14], [180, X644X11B, 32000, 1], [181, E460X80G, 15000, 1], [182, 60FBH00, 10000, 1], [183, 52DBX00, 45000, 1], [184, SE460X11B, 15000, 2], [185, 52D0Z00, 100000, 3], [186, S60FBX00, 20000, 2], [187, ST654X11B, 36000, 2], [188, T650A11B, 7000, 1], [189, ST64038HL, 32000, 2], [190, S50FBX00, 10000, 2], [191, S12A7465, 32000, 2], [192, S50FBU00, 20000, 2], [193, S60F4H00, 10000, 2], [194, S52D4X00, 45000, 2], [195, E260X22G, 30000, 3], [196, C925H2MG, 7500, 14], [197, C925H2YG, 7500, 14], [198, C925H2CG, 7500, 14], [199, C925H2KG, 8500, 14], [200, 60FBX0E, 20000, 1], [201, C925X76G, 30000, 9], [202, SX203H22G, 25000, 4], [203, X264H11G, 9000, 1], [205, T664420 - Amarelo, 6500, 10], [206, T664320 - Magenta, 6500, 10], [207, T664220 - Ciano, 6500, 10], [208, T664120 - Preto, 4000, 10], [209, 64080HW, 21000, 1], [210, 841586, 10000, 1], [211, 841501, 9500, 5], [212, 841503, 9500, 5], [213, 841502, 9500, 5], [214, D0392040, 80000, 3]]

My need is to make this string into a array to get access to the indexes of each subarray of that array main, for example to get index 1 of the 1st sub array, it would be something like array 0 , something like this.

I was able to better filter how the string is being returned, now it looks like this:

[["0250A002ADST"],["T664420 - Amarelo"],["SE260X22G"],["841586"],["C734X24G"],["841501"],["841502"],["841503"],["Q7553A"],["X264H11G"],["E260X22G"],["C540X74G"],["12A8302"],["X203H22G"],["C53034X"],["50F0Z00"],["52D0Z00"],["SX203H22G"],["SE250X22G"],["Q2682A"],["C5340CX"],["C5340YX"],["C5340MX"],["C5240MH"],["C930H2CG"],["C930H2MG"],["C930H2YG"],["X945X2MG"],["X945X2YG"],["C544X1CG"],["C544X1MG"],["C544X1YG"],["2448B003AA"],["2449B003AA"],["2450B003AA"],["Q2681A"],["Q2683A"],["Q3961A"],["Q3962A"],["Q3963A"],["Q6001A"],["Q6002A"],["Q6003A"],["X945X2CG"],["C5220CS"],["C5220MS"],["C5220YS"],["C5240CH"],["C540H1YG"],["C736H1YG"],["C736H1MG"],["C736H1CG"],["70C8XC0"],["70C8XM0"],["70C8XY0"],["C5240YH"],["C748H1YG"],["C748H1MG"],["C748H1CG"],["X748H1CG"],["C746H1KG"],["SE450H11L"],["SX203A11G-6K"],["D0392040"],["SE460X11B"],["S50FBX00"],["ST64038HL"],["ST654X11B"],["SE352H11B"],["T650A11B"],["64080HW"],["Q5949A"],["C5240KH"],["34018HL"],["Q5949X"],["Q6511A"],["Q7553X"],["12A7415"],["64418XL"],["C930H2KG"],["64018HL"],["X644X11L"],["0387B003AA"],["113R00635"],["12A6160"],["18S0090"],["MLT-D209L"],["MLT-D209L 3K"],["Q2613A"],["Q2670A"],["Q3960A"],["Q6000A"],["Q6511X"],["T654X11L"],["12A7400"],["E450A11L"],["E260A11L"],["C5220KS"],["E460X11L"],["T650H11L"],["TN460"],["TN570"],["TN670"],["X463X11G"],["X654X11L"],["SML-D2850B"],["X945X2KG"],["C736H1KG"],["C782X1KG"],["E460X80G"],["LS64X"],["T654X80G"],["12A7465"],["50F4U00"],["50F4X00"],["50FBU00"],["52D4H00"],["60F4X00"],["60FBH00"],["60FBX0E"],["64018HB"],["64418XB"],["E460X11B"],["60FBX00"],["X463X11B"],["X654X11B"],["50FBX00"],["SX264H11G"],["S12A7465"],["S50FBU00"],["X340H22G"],["C930X72G"],["S60FBX00"],["T654X11B"],["X644X11B"],["12A7610"],["S60F4H00"],["X203A11G"],["SE352H11L"],["SX340H11G"],["12A4715"],["12A3160"],["12A8400"],["C4092A"],["TN560"],["C4096A"],["C4127X"],["12B0090"],["1390A003AA"],["13T0101"],["2447B003AA"],["C3903A"],["C6650FL"],["C7115X"],["C8061X"],["Q2610A"],["Q2612A"],["Q2613X"],["S52D4X00"],["Q3964A"],["C930X73G"],["C544X1KG"],["C734A1KG"],["64480XW"],["12A5845"],["12A6835"],["W84020H"],["52DBX00"],["70C8XK0"],["X748H1MG"],["X748H1YG"],["C4810A"],["C4811A"],["C4812A"],["C4813A"],["C9381A"],["C9382A"],["0251A001AA"],["1007B001AA"],["6707A001AC"],["25A0013"],["C540X75G"],["C930X76G"],["C734X77G"],["C52025X"],["C925X76G"],["T664220 - Ciano"],["T664320 - Magenta"],["T664120 - Preto"],["C4844A"],["51645A"],["CARTUCHO HP TESTE"],["SCX-D4200A"],["51645GL"],["CN053AL"],["X746H1KG"],["C6578A"],["C4836A"],["C4837A"],["C4838A"],["C1823T"],["C6578D"],["C1823DL"],["C925H2MG"],["C925H2CG"],["CN054AL"],["CN055AL"],["CN056AL"],["C925H2KG"],["C925H2YG"]]

I’ve done this to separate this string:

var x = $data.listaSuprimentos.value;
var json = JSON.parse(x);
return json;

But it’s bringing ][" together into dropdown :

    

Answer :

From what I see in this array [["0250A002ADST"],["T664420 - Amarelo"]... each sub-array has only one string. So you need to map this array to remove each subarray and leave in its place the string it has inside. Or, by having the sub-array ["0250A002ADST"] you can extract that first (and only) position with [0] .

You can do it like this:

const json = '[["0250A002ADST"],["T664420 - Amarelo"]]';
const arr = JSON.parse(json);
const strings = arr.map(el => el[0]);
console.log(strings);

    

Try to improve your return and give Split some specific character, something like this:

 var array = sua_string.split(",");
    var v1 = array[0];
    var v2= array[1];   

    

Based on the principle that the string is correctly formatted, you can parse for JSON:

let str = '[[1, "X340H22G", 30000, 3],[2, "64418XL", 32000,1], [3, "X644X11L", 32000, 1]]';
let json = JSON.parse(str);

// acessar o conteudo de cada subarray
json.forEach((item) => console.log(item[1]));

    

Leave a Reply

Your email address will not be published. Required fields are marked *