Home > Mobile >  I am unable to create this logic. Need to add index and subIndex fields dynamically
I am unable to create this logic. Need to add index and subIndex fields dynamically

Time:02-10

I am unable to create this logic, trying for hours. I just need to add index and subIndex properties with increments and on behave of different eventTicketId.

For a single ticket, for example eventTicketId:461. I have to have 2 fields like first ticket and its first value and then first ticket and its second value. Same goes for other tickets like for eventTicketId:463 it will go like first ticket and its first value, first ticket and its second value, second ticket and its first value and at last second ticket and its second value.

Orignal Array:

 let ticketArray = [
 {
    "eventTicketId": "461",
    "title": "Name 1",
    "value": "salman",
    "type": 'name'
 },
{
    "eventTicketId": "461",
    "title": "CNIC 1",
    "value": "31221321",
    "type": 'cnic'

},
{
    "eventTicketId": "462",
    "title": "Name 1",
    "value": "asdfasdf",
    "type": 'name'

},
{
    "eventTicketId": "462",
    "title": "CNIC 1",
    "value": "31221321",
    "type": 'cnic'

},
{
    "eventTicketId": "463",
    "title": "Name 1",
    "value": "Adsadsa",
    "type": 'name'

},
{
    "eventTicketId": "463",
    "title": "CNIC 1",
    "value": "321312312",
    "type": 'cnic'
},
{
    "eventTicketId": "463",
    "title": "Name 2",
    "value": "Adsadsa",
    "type": 'name'

},
{
    "eventTicketId": "463",
    "title": "CNIC 2",
    "value": "133213",
    "type": 'cnic'
}];

Required Array:

 let ticketArray = [
 {
    "eventTicketId": "461",
    "title": "Name 1",
    "value": "salman",
    "type": 'name',
    "index": "1",
    "subIndex": "1"
}, {
    "eventTicketId": "461",
    "title": "CNIC 1",
    "value": "31221321",
    "type": 'cnic',
    "index": "1",
    "subIndex": "2"
},
{
    "eventTicketId": "462",
    "title": "Name 1",
    "value": "asdfasdf",
    "type": 'name',
    "index": "1",
    "subIndex": "1"


},
{
    "eventTicketId": "462",
    "title": "CNIC 1",
    "value": "31221321",
    "type": 'cnic',
    "index": "1",
    "subIndex": "2"

},
{
    "eventTicketId": "463",
    "title": "Name 2",
    "value": "Adsadsa",
    "type": 'name',
    "index": "1",
    "subIndex": "1"

},
{
    "eventTicketId": "463",
    "title": "CNIC 2",
    "value": "133213",
    "type": 'cnic',
    "index": "1",
    "subIndex": "2"
},

{
    "eventTicketId": "463",
    "title": "Name 1",
    "value": "Adsadsa",
    "type": 'name',
    "index": "2",
    "subIndex": "1"
},
{
    "eventTicketId": "463",
    "title": "CNIC 1",
    "value": "321312312",
    "type": 'cnic',
    "index": "2",
    "subIndex": "2"
},];

CodePudding user response:

I made a snippet with what I think would be a possible answer to your problem.

 let ticketArray = [
 {
    "eventTicketId": "461",
    "title": "Name 1",
    "value": "salman",
    "type": 'name'
 },
{
    "eventTicketId": "461",
    "title": "CNIC 1",
    "value": "31221321",
    "type": 'cnic'

},
{
    "eventTicketId": "462",
    "title": "Name 1",
    "value": "asdfasdf",
    "type": 'name'

},
{
    "eventTicketId": "462",
    "title": "CNIC 1",
    "value": "31221321",
    "type": 'cnic'

},
{
    "eventTicketId": "463",
    "title": "Name 1",
    "value": "Adsadsa",
    "type": 'name'

},
{
    "eventTicketId": "463",
    "title": "CNIC 1",
    "value": "321312312",
    "type": 'cnic'
},
{
    "eventTicketId": "463",
    "title": "Name 2",
    "value": "Adsadsa",
    "type": 'name'

},
{
    "eventTicketId": "463",
    "title": "CNIC 2",
    "value": "133213",
    "type": 'cnic'
}];

const tickets = {};
const result = ticketArray.map(ticket => {
  let subIndex = 1
  if (tickets[ticket.eventTicketId]) {
    tickets[ticket.eventTicketId]  = 1
  } else {
    tickets[ticket.eventTicketId] = 1
  }
  let index = Math.ceil(tickets[ticket.eventTicketId] / 2);
  subIndex = tickets[ticket.eventTicketId] % 2 == 0 ? 2 : 1
  return {...ticket, index, subIndex}
})

console.log(result)

  • Related