I have an array of objects with the following structure:
patents: [
{
id: 1,
patentTitle: 'cling'
p3sServices: [
{
action: 'file',
color: 'green'
}
]
},
{
id: 2,
patentTitle: 'mirror
p3sServices: [
{
action: 'renew',
color: 'amber'
},
{
action: 'give',
color: 'blue'
}
]
}
]
I have the following code which loops through the nested array p3sServices, and creates and pushes an object the length of the `p3sServices'. So if it is only one item, once, if it is two items, twice.
function sortPatents(patents)
var obj = {
Green: [],
Amber: [],
Red: [],
Blue: [],
Black: [],
Grey: []
}
patents.forEach(function(patent){
patent.p3sServices.forEach(function(action, idx){
var string = action.currentStageColour.toLowerCase(); //SELECT COLOUR
var capitlized = string.charAt(0).toUpperCase() + string.slice(1);
//BEFORE THIS PUSH I NEED TO SPLICE/FILTER P3SSERVICES ARRAY
obj[capitlized].push(patent); //PUSH PATENT TO APPROPRIATE ARRAY IN OBJ
})
})
}
However, on push, I need to filter out the other item in p3sServices. So the above object item with id 2,if I looped through p3sServices array an was currently on index 0 with action: 'renew' and color: 'amber, I'd want to filter out index 1 (action: 'give'). Then when the loop is on index 1, I want to filter out index 0 (action: 'renew' and color: 'amber). I want the array to look like this afterwards:
newPatents: [
{
id: 1,
patentTitle: 'cling'
p3sServices: [
{
action: 'file',
color: 'green'
}
]
},
{
id: 2,
patentTitle: 'mirror
p3sServices: [
{
action: 'renew',
color: 'amber'
},
]
},
{
id: 2,
patentTitle: 'mirror
p3sServices: [
{
action: 'give',
color: 'blue'
}
]
}
]
Question
How would I achieve creating duplicates of the objects based on the nested array p3sServices, and then deleting the other item in the array before pushing, and repeating this process for the next itemin the array? Apologies if this doesn't make sense.