0

I was wondering if it's possible to somehow make array/object's children to be exportable into another .js files

Let's say, I have a file a.js with this code:

const vector = ['a', 'b', 'c'];
export default vector;

Array's elements may be functions, collections, whatever...

Is there a way I could do the following in my file b.js?

import {c} from './a.js';
// do something with c
Le garcon
  • 7,197
  • 9
  • 31
  • 46

1 Answers1

0

If you do:

const vector = ['a', 'b', 'c'];
export default vector;

Then, when you import, you have to reference the array element by number or use some other array method on it. Arrays are not indexed by value, they are accessed by their index.

import vector from '/a.js';
console.log(vector[2]);            // 'c'
console.log(vector.indexOf('c'));  // 2

Is there a way I could do the following in my file b.js? import {c} from './a.js';.

No. What you exported has no c property. In fact, the array you exported has no custom properties at all, just the array elements which are accessed like any array element would be.

jfriend00
  • 683,504
  • 96
  • 985
  • 979