Home > Net >  how to get to the properties of an object inside of it javascript
how to get to the properties of an object inside of it javascript

Time:06-28

I would like to get 'experiments[0].name' but I have error that name is not defined. How to get this property?

let experiments = [ {name: "C200929R01_SizeMarker", file: experiments[0].name ".xrrx"} ];

CodePudding user response:

Until that closing ] experiments indeed won't be defined.

If your just trying to save typing the name in twice, you could retructure like ->

const name = "C200929R01_SizeMarker";
let experiments = [ {name, file: name   ".xrrx"} ];

console.log(experiments);

CodePudding user response:

let experiments = [ {name: "C200929R01_SizeMarker", file: experiments[0].name   ".xrrx"} ];

When you are asigning file: experiments[0].name, experiments[0] is not defined yet, so you have different options:

let experiments = [{name: "C200929R01_SizeMarker"}];
experiments[0].file = experiments[0].name   ".xrrx";

Or using the option described in the previous answer

  • Related