I want to export a variable called recettesFromIngredients
from search-box.js
to ingredients-logic.js
.
So to be more precise recettesFromIngredients
is a variable with an empty array at first.
But then I fill this empty array here:
function ingredients(enteredValue) {
for (let i = 0; i < recettes.length; i ++) {
for (let d = 0; d < recettes[i].ingredients.length; d++) {
if ( recettes[i].ingredients[d].ingredient.toLocaleLowerCase().includes(enteredValue)) {
recettesFromIngredients.push(recettes[i]); // <-----------
};
};
};
};
So what I am trying to do is: once I fill this array I want to export it.
This is what I tried to do
export {recettesFromIngredients} // at the bottom of the file after the every function is called
In the console I don't have any error the export/import seems to be ok but when I console log this Var in the imported file I got an empty array while in the export file the array is filled as wanted!
What could be the problem that I am getting an empty array in ingredients-logic.js
? Should I export from another position in the file or in another way?
Why don't you try to export the function instead? Then you can call the function in another file, the function will return the filled array. You have to return the array in the function call as well. Your code should look like:
export default function ingredients(enteredValue) {
let recettesFromIngredients = [];
for (let i = 0; i < recettes.length; i ++) {
for (let d = 0; d < recettes[i].ingredients.length; d++) {
if ( recettes[i].ingredients[d].ingredient.toLocaleLowerCase().includes(enteredValue)) {
recettesFromIngredients.push(recettes[i]); // <-----------
};
};
};
return recettesFromIngredients
};
import the function in another file, then call the function
const x= ingredients(enteredValue)
now const x has the array
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.