Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

142
Views
VueJS - Push an object to an array of objects inside an async function

I'm currently working on a Vue 2 project, and I have to push an object from outside the async function scoop. Here's the code in subject below :

async getData() {
  return [
  {
    id: "1",
    name: "Slim",
    age: 5,
    location: 1500,
    breed: 500,
  },
  {
    id: "2",
    name: "Sol",
    age: 3,
    location: 1500,
    breed: 1,
  },
};

Is there a way to access and push the data of this array of objects from outside of the getData() scoop

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

I would suggest you declare an array outside the function scope and then add changes to it, or you can pass the update array as parameter.

let array = [
  {
    id: "1",
    name: "Slim",
    age: 5,
    location: 1500,
    breed: 500,
  },
  {
    id: "2",
    name: "Sol",
    age: 3,
    location: 1500,
    breed: 1,
  }];

//pass array as a parameter
async getData(array){
    return array
}

//update your array
array.push("your object");

//pass updated array in function call
getData(array);
about 4 years ago · Juan Pablo Isaza Report

0

You can also use this approach,

async function getData(item) {
  return [
  {
    id: "1",
    name: "Slim",
    age: 5,
    location: 1500,
    breed: 500,
  },
  {
    id: "2",
    name: "Sol",
    age: 3,
    location: 1500,
    breed: 1,
  },
  item
  ]
};

getData(  {
    id: "3",
    name: "obj",
    age: 5,
    location: 1500,
    breed: 1,
  });
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!