-2

Background: In the simplified data structure below accessing individual items and their properties is straightforward. For example the value Volkswagon is easy to access.

let car = {};
let truck = {};

car.one = 'Volkswagon';
car.two = 'Toyota';

truck.one = 'Dakota';
truck.two = 'Tacoma';

let vehicleArray = [car, truck];

console.log(vehicleArray[0].one);  //Volkswagon

Question: However is it possible to access the name's of the objects that are stored in the array? In this case the values car and truck. I'm not sure how to get those out of the array.

The code below returns properties and values inside the object but not the object's name.

console.log(vehicleArray[0]); // { one: 'Volkswagon', two: 'Toyota' }
  • No. If a thing's name matters like that, don't use an array. – Jared Smith Sep 08 '19 at 02:02
  • Possible duplicate of [Javascript, refer to a variable using a string containing its name?](https://stackoverflow.com/questions/1664282/javascript-refer-to-a-variable-using-a-string-containing-its-name) – Jared Smith Sep 08 '19 at 02:03
  • 3
    Those aren't the "names" of the objects; they're variables to which a reference has been assigned. The objects don't "know" the variables exist. – Pointy Sep 08 '19 at 02:04
  • 1
    Indeed, to further what Pointy says consider: `var foo = { a: 1 }; var bar = foo;` should the name be `foo` or `bar`? – Jared Smith Sep 08 '19 at 02:05
  • @JaredSmith Very interesting conundrum. In that case bar.a will return 1. But dang, but there is no way to pull foo out dynamically? Time to think about a restructure. – stackedAndOverflowed Sep 08 '19 at 02:10
  • 2
    This is an [XY Problem](http://xyproblem.info/). Explain your use case in more detail – charlietfl Sep 08 '19 at 02:14
  • @stackedAndOverflowed can't do it with vars. Again, if you need to be able to refer to something dynamically by name, put it in an object: `var someObj = { foo: { a: 1 } }; var bar = 'foo'; someObj[bar].a === 1;` – Jared Smith Sep 08 '19 at 17:29

2 Answers2

-1

You can map over the array like this to retrieve all the values:

let car = {};
let truck = {};

car.one = 'Volkswagon';
car.two = 'Toyota';

truck.one = 'Dakota';
truck.two = 'Tacoma';

let vehicleArray = [car, truck];

vehicleArray.map(({ one, two }) => console.log(one, two))

Or could filter to get a specific value:

let car = {};
let truck = {};

car.one = 'Volkswagon';
car.two = 'Toyota';

truck.one = 'Dakota';
truck.two = 'Tacoma';

let vehicleArray = [car, truck];

console.log(vehicleArray.filter(item => item === truck)[0].one)

This works by checking the object equality. Since truck and vehicleArray[1] are referencing the same object.

skovy
  • 5,430
  • 2
  • 20
  • 34
  • 2
    This does not answer the question: OP wants to dynamically get the variable names e.g. *car* and *truck*. – Jared Smith Sep 08 '19 at 02:04
  • 1
    Thanks @skovy but Jared Smith is correct. I'm trying to pull out the values car and truck dynamically without knowing they are inside. – stackedAndOverflowed Sep 08 '19 at 02:12
  • The question has always stated that he wanted to get the values car and truck. – Herohtar Sep 08 '19 at 02:16
  • What's the use case? Could you add a `type` or `name` attribute to `car` and `truck`? – skovy Sep 08 '19 at 02:17
  • Using map incorrectly. It is the wrong tool for the job when all you really want is forEach. Similarly `find()` would be more appropriate than `filter()` – charlietfl Sep 08 '19 at 02:25
-2

If an object is stored in an array is it possible to view/retrieve the object's name from the array? No you can not...

If you wrap your objects with the name and push them to array you can get the names back

let car = {};

let truck = {};

car.one = 'Volkswagon';

car.two = 'Toyota';

truck.one = 'Dakota';

truck.two = 'Tacoma';

vehicleArray = [{car}, {truck}];

vehicleArray.map(v => console.log(Object.keys(v)[0]));
Sajid k.k
  • 1
  • 4