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.

1
  • 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? Commented Feb 5, 2021 at 17:13

1 Answer 1

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])

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.