0

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.

1 Answer 1

2

You could create copies of each patent, one for each service:

const unpackedServices = newPatents.map(patent => patent.p3sServices.map(serv => ({
  id: patent.id,
  patentTitle: patent.patentTitle,
  p3sServices: [serv]
})

This will give you an array of arrays. In each nested item you will have one patent/service pair.

If you want to flatten the array, you use concat:

const result = [].concat(...unpackedServices);

Hope it helps.

Sign up to request clarification or add additional context in comments.

1 Comment

Exactly what I wanted! Bit of tweaking but it's all working now. Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.