I am trying to convert an array that looks like the one below, into the structure of the object below the array.
I have tried a number of things, which I will list below as well. But I feel I am going about it the wrong way. And would like some assistance with this.
var array = [
["plan_a", "tester_a", "product", "foo", "blocked", "5"],
["plan_a", "tester_a", "product", "foo", "passed", "10"],
["plan_a", "tester_a", "subproduct", "client", "passed", "15"],
["plan_a", "tester_b", "product", "bar", "blocked", "5"],
["plan_a", "tester_b", "product", "bar", "passed", "10"],
["plan_a", "tester_b", "subproduct", "server", "failed", "5"],
["plan_a", "tester_b", "subproduct", "server", "passed", "5"],
["plan_a", "tester_c", "product", "foo", "failed", "5"]
];
var object = {
plan_a: {
tester_a: {
product: {
product: "foo",
blocked: "5",
passed: "10"
},
subproduct: {
subproduct: "client",
passed: "15"
}
},
tester_b: {
product: {
product: "bar",
blocked: "5",
passed: "10"
},
subproduct: {
subproduct: "server",
failed: "5",
passed: "5"
}
},
tester_c: {
product: {
product: "foo",
failed: "5"
}
}
}
};
I have tried this:
var object = {}
var combining_plans =[]
var first_row = arr[0][0];
for (var i = 0; i < arr.length; i++) {
if (arr[i][0] == first_row) {
object[arr[i][0]] = [];
arr[i].shift();
combining_plans.push(arr[i]);
}
}
object[first_row] = combining_plans;
This checks the first column for the same if the same it takes the first element turns it into the first key of the object and then adds the values as all the remaining elements of the array.
I was thinking about doing that over and over for every column, but I feel that may not work, and there is a more elegant way than to do this.