//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
CodePudding user response:
You JSON.parse
which return array and Spread operator like this
var env1ParsedScriptOutput='[{"somevalue":"stringified_jsonOuput1"},{"somevalue":"stringified_jsonOuput1"}]';
var env2ParsedScriptOutput='[{"somevalue":"stringified_jsonOuput2"},{"somevalue":"stringified_jsonOuput2"}]';
var env3ParsedScriptOutput='[{"somevalue":"stringified_jsonOuput3"},{"somevalue":"stringified_jsonOuput3"}]';
let results = [
...JSON.parse(env1ParsedScriptOutput),
...JSON.parse(env2ParsedScriptOutput),
...JSON.parse(env3ParsedScriptOutput)
];
console.log(results);
CodePudding user response:
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);