I am trying to make a simple project of 2 files and I am sure I am missing a simple thing :-(
For some reason, the import is passing but the imported array is showing error when I try to us an array function (filter), and the content of it I see when I print is empty brackets {}.
var zipcode = [
{
"code": 1451,
"city": "Harvard"
},
{
"code": 1503,
"city": "Berlin"
},
{
"code": 1505,
"city": "Boylston"
}
]
export default zipcode;
import zipcode from './';
var valid_code = zipcode.filter(function(zip){
return (zip.code === 1505);
});
var valid_code = _2.default.filter(function (zip) { ^ TypeError: _2.default.filter is not a function
What am I missing on the import procedure / concept? Thanks in advance :-)
So, although this thread is almost 3 years old, for anyone who might encounter this "problem" in the future I post the hopefully correct answer. "Problem" is in quotes for good reason because this is not a real problem in itself but rather a misunderstanding which I happened to also come across:
When you write
import zipcode from './zipcode'
or
const zipcode = require('./zipcode')
then you import a module. It is and was confusing - for me as well - because the array as well as the file/module are named the same. So with your writing, you need to access the "zipcode" property of your "zipcode" module that you imported resulting in a call like this:
zipcode.zipcode.filter(...)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With