1

I have this object

let listItem = {
  name: "Lydia",
  accountId: "HGVe",
  fleetType: "Haulage"
};

I want to get an array which will be exactly like this:

[listItem.name, listItem.accountId, listItem.fleetType]

The values in the array don't have to be strings or values as you can see.

So every element of the array is going to be VariableName.key (not a string).

!!!!To be 100% clear, these 2 results are NOT what I need:

["listItem.name", "listItem.accountId", "listItem.fleetType"] // results are strings: WRONG
["Lydia", "HGVe", "Haulage"] // results are values: WRONG

As you can see above, the correct array have in each element a reference to the precise object.key

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • 5
    `Object.values(listItem)`. – ASDFGerte Oct 02 '19 at 15:34
  • Are you saying to want the array to be `['name', 'accountId', 'fleetType']` or `['Lydia', 'HGVe', 'Haulage']` ? – Taplar Oct 02 '19 at 15:35
  • 1
    Possible duplicate of [How to get all properties values of a Javascript Object (without knowing the keys)?](https://stackoverflow.com/questions/7306669/how-to-get-all-properties-values-of-a-javascript-object-without-knowing-the-key) – ASDFGerte Oct 02 '19 at 15:41
  • No, I edited the question for clarification. – Alessandro Catania Oct 03 '19 at 12:47
  • So you are wanting to get an array of objects based off the object type and field name? – Tony Abrams Oct 03 '19 at 12:48
  • Looks like an [XY problem](https://meta.stackexchange.com/q/66377/342076). Tell us what you want to do by storing those references in `VariableName.key` format. – Nikhil Oct 03 '19 at 14:29

1 Answers1

1

Based on your edit, you want to store object references of listItem object's properties.

let listItem = {
  name: "Lydia",
  accountId: "HGVe",
  fleetType: "Haulage"
};

But this object's properties are not objects but primitive values. Primitive values don't have a reference. See this answer for more information.

You can create a string object using String constructor. And use Object.values() to get these values.

let listItem = {
  name: new String("Lydia"),
  accountId: new String("HGVe"),
  fleetType: new String("Haulage")
};

let refs = Object.values(listItem);

console.log(refs);

You now have references stored in an array. You can't have [listItem.name, listItem.accountId, listItem.fleetType] without them getting evaluated to those references.


Previous Answer:

You can wrap this object in another object and use Object.keys() to get its name.

Then use Object.keys() on original object to get its keys.

let listItem = {
  name: "Lydia",
  accountId: "HGVe",
  fleetType: "Haulage"
};

var obj = { listItem };
var name = Object.keys(obj)[0];
var result = [];

Object.keys(listItem).forEach((key) => {
  result.push(name + "." + key);
});

console.log(result);

So every element of the array is going to be VariableName.key (not a string).

Every value should have a data type. So what would be the data type of these values? They are likely to be strings.

Nikhil
  • 6,493
  • 10
  • 31
  • 68
  • Thanks @Nikhil, but this code return an array of string. I do not need string, I need the reference to each value with the object reference as prefix, I edited my question to clarify more. – Alessandro Catania Oct 03 '19 at 12:42
  • @AlessandroCatania - See my edited answer. You can get the references of the values as shown in the answer. But you can't have that name prefix because `VariableName.key` gets evaluated and what gets stored is the reference which it points to. – Nikhil Oct 03 '19 at 14:27