60

Object.values() received following error:

TypeError: Object.values is not a function.

From this question on stackoverflow - I see that Object.values() is not supported in all browsers.

But I am using the function in Node.js on server side - How can I use Object.values() in Node.js it seems so intuitive like Object.keys()?

6
  • "But I am using it on server side code" - using Node.js? Commented Nov 4, 2016 at 11:46
  • question was to find how to use Object.values() in node.js as mentioned in title. I have edited question to make it explicit in body as well. why the downvote its not the duplicate ? Commented Nov 4, 2016 at 11:55
  • I didn't downvote. Due to the unspecific nature, I thought it was a duplicate, which has now been retracted. Commented Nov 4, 2016 at 11:57
  • as mentioned previously it wasn't unspecific - title clearly states the intent Commented Nov 4, 2016 at 11:59
  • 2
    I'm using node version node -v v9.8.0 and I still get that same error. Commented Apr 20, 2018 at 15:50

8 Answers 8

74

Object.values is a new feature in ES2017. It is very bleeding edge. Node.js has full support for it from version 7.0.

6.8.1 supports it, but it is considered unstable and is locked behind the --harmony flag.

You can either:

  • Upgrade to the latest Node.js LTS and use --harmony
  • Upgrade to the latest Node.js Current
  • Use a polyfill
Sign up to request clarification or add additional context in comments.

1 Comment

Use node -v to find out what version of Node you're using.
21

Object.values() is in status "Draft" for the version ECMAScript2017 and here the specification: ECMAScript 2017 Draft (ECMA-262) The definition of 'Object.values' in that specification..

The Object.values() method returns an array of a given object's own enumerable property values, in the same order as that provided by a for...in loop (the difference being that a for-in loop enumerates properties in the prototype chain as well).

Without change nothing in your NodeJS enviroment you can achive the same by using Object.keys() that returns an array of keys and chaining a Array.prototype.map() method to return the desired array of the Object's values:

const obj = { 
    foo: "bar", 
    baz: 42 
  },
  // Object.values()
  objValues = Object.values(obj),
  // Object.keys() and map(),
  objKeysMap = Object.keys(obj).map((k) => obj[k]);

console.log('objValues:', objValues);
console.log('objKeysMap:', objKeysMap);

Comments

12

Lodash is a pretty awesome tool to simplify your JavaScript code. You can use _.values:

var _ = require('lodash');
_.values({ a: 'a' }) // => ['a']

Comments

11

I am developing a AWS Lambda and now I stumbeled over the same problem and I solved it by this simple line (assuming a is the object) const values = Object.keys(a).map(k => a[k]);

1 Comment

You should check if it's not already defined for future proofing.. we're actually already in the future where it exists in newer node versions :) No reason to overwrite it when it's already defined.
4

3 alternative to go around if you don't want to upgrade node

  1. mapping keys as in this answer
  2. using lodash as another answer by @sakovias
  3. using this object.values npm package

Comments

4

Here's a polyfill that kicks in only when it's not already defined:

const objectToValuesPolyfill = (object) => {
  return Object.keys(object).map(key => object[key]);
};
Object.values = Object.values || objectToValuesPolyfill;

Comments

0

Upgrade your Node.js to the latest version. It'll enable you to use

Object.values()

You can get it from the official site: https://nodejs.org/en/download/current/

It worked for me :)

Comments

0

Or use underscore.js

var _ = require('underscore');

const obj = { 
    foo: "bar", 
    baz: 42 
  };

console.log(_.values(obj));
// ["bar",42]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.