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
CodePudding user response:
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);
CodePudding user response:
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,
});
