1

So i have an array of objects which have some data like so:

[
 {
   id:4390,
   name:panadol
 },
{
   id:4392,
   name:aspirin
 },
{
   id:1390,
   name:vantage
 },
{
   id:2390,
   name:cream
 },
]

Currently they are being displayed in the same order i would say or roughly the same there is too much data so. anyways say another array of objects is fetched from the server after searching with keyword and that looks like this:

[
 {
   id:3390,
   name:morphine
 },
{
   id:3231,
   name:flagyl
 },

]

So after this array is merged in the orignal array how can i sort it so that the newest data is displayed first.

Hadi Pawar
  • 1,090
  • 1
  • 11
  • 23
  • doesn't that depend on the code you use to display this array? Are you just asking how to merge these two arrays such that the second is at the beginning? – Christian Fritz Feb 05 '21 at 17:13

1 Answers1

1

If you are looking to merge two arrays while putting values of the second one at the beginning, you can use destructuring:

arr1 = [
  {
   "id":4390,
   "name":"panadol"
 },
{
   "id":4392,
   "name":"aspirin"
 },
{
   "id":1390,
   "name":"vantage"
 },
{
   "id":2390,
   "name":"cream"
 },
]

arr2 = [
 {
   "id":3390,
   "name":"morphine"
 },
{
   "id":3231,
   "name":"flagyl"
 },

]

console.log([...arr2, ...arr1])
RobBlanchard
  • 855
  • 3
  • 17