-
-
Save carlosvega20/804889ef2e187dbcfadc to your computer and use it in GitHub Desktop.
Data Transformation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var x = [ {node: "ip2", app: "dbsyncer", containerId: "1234"}, | |
{node: "ip2", app: "logforwa", containerId: "3423"}, | |
{node: "ip4", app: "dbsyncer", containerId: "2213"}, | |
{node: "ip4", app: "logforwa", containerId: "3434"} ] | |
var newObj = {}; | |
x.forEach(function (item) { | |
newObj[item.node] = newObj[item.node] || []; | |
newObj[item.node].push(item); | |
item.node = undefined | |
}) | |
console.log(JSON.stringify(newObj)); | |
var result = []; | |
for (i in newObj) { | |
if (newObj.hasOwnProperty(i)) { | |
var node = newObj | |
result.push({i: newObj[i]}) | |
} | |
} | |
// Henry's Solution | |
var myObj = {} | |
x.forEach(function (item) { | |
if (myObj[item.node]) { | |
myObj[item.node].push({app: item.app, containerId: item.containerId}) | |
} else { | |
myObj[item.node] = [] | |
myObj[item.node].push({app: item.app, containerId: item.containerId}) | |
} | |
}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment