This could be a simple solution but I can't figure out if I can use this method or not. This question is asked before in different ways but I am looking through object methods not functions so bear with me please.
Is this possible to access objects properties and return them as an array through Object.getOwnPropertyNames(object)?
For example , this method working fine with below variable without array.
var y = {
firstName: "John",
lastName: "Doe",
eyeColor: "Black",
age: 50
};
console.log(
Object.getOwnPropertyNames(y)
);
Returns
(4) ["firstName", "lastName", "eyeColor", "age"]
can we use same method to access object properties inside array ?
var x = [{
firstName: "John",
lastName: "Doe",
eyeColor: "Black",
age: 50
}, {
firstName: "Marry",
lastName: "Smith",
eyeColor: "Brown",
age: 45
}];
-
Will all the objects in the array have the same structure? also are all properties mandatory? this is quite important and some of the answers make assumptions as to the answer of this question without asking it first.Damo– Damo2019年01月06日 14:16:21 +00:00Commented Jan 6, 2019 at 14:16
5 Answers 5
You can use spread operator ... for this:
var x = [{
firstName: "John",
lastName: "Doe",
eyeColor: "Black",
age: 50
}, {
firstName: "Marry",
lastName: "Smith",
eyeColor: "Brown",
age: 45
}];
console.log(
Object.getOwnPropertyNames(...x)
);
1 Comment
x[0] will suffice?It wouldn’t work, but depending on your desired output structure, you could use map to transform each array element
x.map(Object.getOwnPropertyNames)
would return an array of property name arrays.
Comments
var x = [{firstName: "John", lastName : "Doe", eyeColor : "Black", age : 50}, {firstName: "Marry", lastName : "Smith", eyeColor : "Brown", age : 45}];
x.map(y => console.log(Object.getOwnPropertyNames(y)))
You can access the objects in array with map().
Comments
var propNames = Object.getOwnPropertyNames(x[0]);
x[0] is the first object in the array and since all objects seem to have the same keys, calling the function on the first object will get you the result you want.
Note: This solution assumes there is at least one object in the array, so you might want to check that first before doing x[0] or else you'll get an error.
Example:
var x = [{firstName: "John", lastName : "Doe", eyeColor : "Black", age : 50}, {firstName: "Marry", lastName : "Smith", eyeColor : "Brown", age : 45}];
var propNames = Object.getOwnPropertyNames(x[0]);
console.log(propNames);
2 Comments
If the array of objects contains objects with different properties you can get a list of unique property names thus:
var x = [{
firstName: "John",
lastName: "Doe",
eyeColor: "Black"
}, {
firstName: "Marry",
lastName: "Smith",
age: 45
}];
var arrayOfArrays = x.filter(obj => obj).map(Object.getOwnPropertyNames)
var array = [].concat.apply([], arrayOfArrays);
var uniqueProperties = [...new Set(array)]
console.log(uniqueProperties);
This will not crash if the array is empty, or if the array contains null or undefined. But it does assume that x is an array, empty or otherwise.