Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

is there a betterway to merge json arrays into one

//working with vmware software
//so I can't use any JS library or python(i can but the method is harder there)
var env1ParsedScriptOutput='[{"somevalue":"stringified_jsonOuput1"},{"somevalue":"stringified_jsonOuput1"}]';
var env2ParsedScriptOutput='[{"somevalue":"stringified_jsonOuput2"},{"somevalue":"stringified_jsonOuput2"}]';
var env3ParsedScriptOutput='[{"somevalue":"stringified_jsonOuput3"},{"somevalue":"stringified_jsonOuput3"}]';

function rmvBrackets (){
  var newJson = [];
  for (var i=0; i<arguments.length; i++){
    var txt = arguments[i].replace(/(\[|\])/g,'');
    newJson.push(txt);
  }
  return newJson;
};
var allResults = rmvBrackets(env1ScriptOutput,env2ParsedScriptOutput,env3ParsedScriptOutput).toString();
var allScriptExecParsedOutput = '['+allResults+']'; //final return, this will be processed on another object

can anyone help me and enlightme. this is my code I’ve crafted with try and fail method.
I know there is must be a better way to do thisbut I just started to code with JS

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I would concat it and apply Json.parse to turn it to an array.

Then use flat() to flatten it.

var env1ParsedScriptOutput =
    '[{"somevalue":"stringified_jsonOuput1"},{"somevalue":"stringified_jsonOuput1"}]';
var env2ParsedScriptOutput =
    '[{"somevalue":"stringified_jsonOuput2"},{"somevalue":"stringified_jsonOuput2"}]';
var env3ParsedScriptOutput =
    '[{"somevalue":"stringified_jsonOuput3"},{"somevalue":"stringified_jsonOuput3"}]';

const concat = `[${env1ParsedScriptOutput},${env2ParsedScriptOutput},${env3ParsedScriptOutput}]`;
const output = JSON.parse(concat).flat();

console.log(output);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading