1
let row = warehouse_delivery_transaction.find(x => x.kerry_status_name === 'CCC')
    if (!row) {
      let item = warehouse_delivery_transaction.find(x => x.kerry_status_name === 'BBB')
      if (item) {
        warehouse_delivery_transaction.push({
          code: item.code,
          kerry_status_name: 'CCC',
          location: '',
          status_date: item.status_date
        })
      }
    }

Output before

1.AAA
2.BBB
3.DDD

Output After I push data

1.AAA
2.BBB
3.DDD
4.CCC

I want it to come out like this.

1.AAA
2.BBB
3.CCC
4.DDD
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122

1 Answers1

0

Used Array.prototype.splice() to insert to the warehouse_delivery_transaction.length-1 position

let warehouse_delivery_transaction = [{
    code: 'aa',
    kerry_status_name: 'AAA',
    location: '',
    status_date: 'aa'
  },
  {
    code: 'bb',
    kerry_status_name: 'BBB',
    location: '',
    status_date: 'bb'
  }, {
    code: 'dd',
    kerry_status_name: 'DDD',
    location: '',
    status_date: 'dd'
  }
]

console.log('Before: ', warehouse_delivery_transaction)

let row = warehouse_delivery_transaction.find(x => x.kerry_status_name === 'CCC')
if (!row) {
  //let item = warehouse_delivery_transaction.find(x => x.kerry_status_name === 'BBB')    
  //if (item) {
  warehouse_delivery_transaction.splice(warehouse_delivery_transaction.length - 1, 0, {
    code: 'cc',
    kerry_status_name: 'CCC',
    location: '',
    status_date: 'cc'
  })
  //}
}

console.log('After: ', warehouse_delivery_transaction)
ציפורה
  • 106
  • 4