Can someone help me with this, I am trying to achieve an object like so:
{ "docs": [
{ "_id": "item" },
{ "_id": "item" },
{ "_id": "item" }
] }
So I begin with having an object with a key docs which is an array I will push into
let query = { docs: [] };
i then have list of key value pair items like so, around 80:
{ _id: item }
which i push into docs in a forEach loop
query.docs.push(item);
So when I go and stringify
JSON.stringify(query);
it returns the object but the array is empty.
{ "docs": [] }
I’ve no idea why this is happening. Anyone know an approach to go around this to achieve the desired result? Thanks in advance
Actual code:
let elasticQuery = {
docs: []
};
axios({
method: "post",
headers: {
"Content-Type": "application/json"
},
url: sent2VecUrl,
data: {
selected_text: query
}
}).then(results => {
results.data.response.forEach(item => {
elasticQuery.docs.push({ _id: item });
});
});
console.log("without: " + elasticQuery + " with stringify :" + JSON.stringify(elasticQuery));
This is the server response:

JSON.stringify(elasticQuery)is executed beforeresults.data.response.forEach......, before youpushvalues into theelasticQuery.docsarray.