JSFiddle - React, Tailwind, and code Playground
by KomathiKarunakaran
JavaScript
var mystr = [];
var output = [];
// var blah = expand(mystr, "lastvalue");
var test = [ [ 'login.username', 'Username' ],
[ 'login.password', 'Password' ]];
var array_string = [];
test.map((row) => {
mystr.push(expand(row[0], row[1]))
});
console.log(mystr)
function expand(str, val = {}) {
return str.split('.').reduceRight((acc, currentValue) => {
return { [currentValue]: acc }
}, val)
}
var output = [];
mystr.forEach(function(item) {
console.log(item)
var existing = output.filter(function(v, i) {
return v.name == item.name;
});
if (existing.length) {
var existingIndex = output.indexOf(existing[0]);
console.log('exis', existingIndex)
output[existingIndex].value = output[existingIndex].value.concat(item.value);
} else {
if (typeof item.value == 'string')
item.value = [item.value];
output.push(item);
}
});
//console.dir(output);
//console.log(mystr);