(Also open to a php solution)
If I have an array within an array, how do you re-categorize the parent array based on the child array's objects?
Here is an example of what I would like to achieve:
Say if the original array is:
[
{
"name":"Jade",
"tags":[
"developer"
]
},
{
"name":"Bridge",
"tags":[
"cook"
]
},
{
"name":"Ever",
"tags":[
"artist"
]
},
{
"name":"Noah",
"tags":[
"artist",
"developer"
]
}
]
I want to be able to re-categorize it into:
[
"artist": {
{
{
"name":"Noah",
"tags":[
"artist",
"developer"
]
}
}
"developer": {
{
"name":"Jade",
"tags":[
"developer"
]
},
{
"name":"Noah",
"tags":[
"artist",
"developer"
]
}
},
"cook": {
{
"name":"Bridge",
"tags":[
"cook"
]
},
}
]
Here is what I tried:
(the original array is var data)
var data = [original array]
var newArray = [];
for (var i = 0; i < data.length; i++) {
for (var a = 0; a < data[i].tags.length; a++) {
newArray[] = data[i].tags[a];
if (newArray[i] == data[i].tags[a]) {
newArray[i] = data[i];
}
}
}
My theory is to pop the tag (in tags) into a new array, and if the original object.tags has same value as the new object in the array, pop the original object into the new object.
This approach failed.