Home > Enterprise >  Transform array of objects by condition React JS
Transform array of objects by condition React JS

Time:02-02

I am getting an array of objects from the server in the following format:

[
    {
        "country": "UK",
        "name": "Battery Ltd 1",
        "type": "contact"
    },
    {
        "country": "USA",
        "name": "Technologies Inc. 1",
        "type": "contact"
    },
    {
        "country": "",
        "name": "Jayne Mansfield",
        "type": "representative"
    },
    {
        "country": "China",
        "name": "Technologies Inc. 2",
        "type": "contact"
    },
    {
        "country": "",
        "name": "Dan Borrington",
        "type": "representative"
    },
    {
        "country": "",
        "name": "Susan Reedy",
        "type": "representative"
    }
]

However, I need to iterate over this array of objects and convert it to this format: I want to combine the CONTACT type with the following REPRESENTATIVE object or objects. That is, at the output, I would like to get such an array with arrays:

[
    [
        {
            "country": "UK",
            "name": "Battery Ltd 1",
            "type": "contact"
        }
    ],
    [
        {
            "country": "USA",
            "name": "Technologies Inc. 1",
            "type": "contact"
        },
        {
            "country": "",
            "name": "Jayne Mansfield",
            "type": "representative"
        },
    ],
    [
        {
            "country": "China",
            "name": "Technologies Inc. 2",
            "type": "contact"
        },
        {
            "country": "",
            "name": "Dan Borrington",
            "type": "representative"
        },
        {
            "country": "",
            "name": "Susan Reedy",
            "type": "representative"
        }
    ]
]

CodePudding user response:

You can go through the elements and create a group if element is a contact, and add to the group otherwise:

const data = [
    {
        "country": "UK",
        "name": "Battery Ltd 1",
        "type": "contact"
    },
    {
        "country": "USA",
        "name": "Technologies Inc. 1",
        "type": "contact"
    },
    {
        "country": "",
        "name": "Jayne Mansfield",
        "type": "representative"
    },
    {
        "country": "China",
        "name": "Technologies Inc. 2",
        "type": "contact"
    },
    {
        "country": "",
        "name": "Dan Borrington",
        "type": "representative"
    },
    {
        "country": "",
        "name": "Susan Reedy",
        "type": "representative"
    }
]

const result = data.reduce( (r, e) => (e.type === 'contact' ? r.push([e]) : r[r.length -1].push(e), r), [])
console.log(result)

CodePudding user response:

const array = []
let current = []
let hasRepresentative = false

for (const item of getItems()) {
  if (current.length === 0) {
    current.push(item)
    continue
  }
  if (hasRepresentative && item.type === 'contact') {
    array.push(current)
    current = [item]
    continue
  }
  current.push(item)
  hasRepresentative = true
}
array.push(current)

console.log(array)

function getItems() { return [
    {
        "country": "UK",
        "name": "Battery Ltd 1",
        "type": "contact"
    },
    {
        "country": "USA",
        "name": "Technologies Inc. 1",
        "type": "contact"
    },
    {
        "country": "",
        "name": "Jayne Mansfield",
        "type": "representative"
    },
    {
        "country": "China",
        "name": "Technologies Inc. 2",
        "type": "contact"
    },
    {
        "country": "",
        "name": "Dan Borrington",
        "type": "representative"
    },
    {
        "country": "",
        "name": "Susan Reedy",
        "type": "representative"
    }
]
}

  • Related