The Array
object, as with arrays in other programming languages, enables storing a collection of multiple items under a single variable name, and has members for performing common array operations.
The Object
type represents one of JavaScript's data types. It is used to store various keyed collections and more complex entities. Objects can be created using the Object()
constructor or the object initializer / literal syntax.
The Object.values()
method returns an array of a given object's own enumerable string-keyed property values.
const obj = { foo: "bar", baz: 42 };
console.log(Object.values(obj)); // ['bar', 42]
// Array-like object
const arrayLikeObj1 = { 0: "a", 1: "b", 2: "c" };
console.log(Object.values(arrayLikeObj1)); // ['a', 'b', 'c']
// Array-like object with random key ordering
// When using numeric keys, the values are returned in the keys' numerical order
const arrayLikeObj2 = { 100: "a", 2: "b", 7: "c" };
console.log(Object.values(arrayLikeObj2)); // ['b', 'c', 'a']
// getFoo is a non-enumerable property
const myObj = Object.create(
{},
{
getFoo: {
value() {
return this.foo;
},
},
},
);
myObj.foo = "bar";
console.log(Object.values(myObj)); // ['bar']
Non-object arguments are coerced to objects. Only strings may have own enumerable properties, while all other primitives return an empty array.
// Strings have indices as enumerable own properties
console.log(Object.values("foo")); // ['f', 'o', 'o']
// Other primitives have no own properties
console.log(Object.values(100)); // []