Javascript Is there a way that when I push data it doesn't show output the last line?


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

---------------Answer---------------

You can sort objects by properties. Take a look on Other question. There you can find a good answers from users with nicknames: Triptych, Santosh.

Santosh answer:

function getSortedData(data, prop, isAsc) {
    return data.sort((a, b) => {
        return (a[prop] < b[prop] ? -1 : 1) * (isAsc ? 1 : -1)
    });
}

Triptych answer:

const sort_by = (field, reverse, primer) => {

  const key = primer ?
    function(x) {
      return primer(x[field])
    } :
    function(x) {
      return x[field]
    };

  reverse = !reverse ? 1 : -1;

  return function(a, b) {
    return a = key(a), b = key(b), reverse * ((a > b) - (b > a));
  }
}


//Now you can sort by any field at will...

const homes=[{h_id:"3",city:"Dallas",state:"TX",zip:"75201",price:"162500"},{h_id:"4",city:"Bevery

Hills",state:"CA",zip:"90210",price:"319250"},{h_id:"5",city:"New York",state:"NY",zip:"00010",price:"962500"}];

// Sort by price high to low
console.log(homes.sort(sort_by('price', true, parseInt)));

// Sort by city, case-insensitive, A-Z
console.log(homes.sort(sort_by('city', false, (a) =>  a.toUpperCase()
)));

Previous : How to match two character at a time in regex?
Next : How do I pull last 3 levels from a file path in python?