Skip to content

Instantly share code, notes, and snippets.

@carlosvega20
Forked from hzhu/.js
Created September 24, 2015 21:56
Show Gist options
  • Save carlosvega20/804889ef2e187dbcfadc to your computer and use it in GitHub Desktop.
Save carlosvega20/804889ef2e187dbcfadc to your computer and use it in GitHub Desktop.
Data Transformation
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