Create an array based on the keys of a JavaScript object

Create an array based on the keys of a JavaScript object

javascript object keys values
javascript object get value by key
javascript get value by key in array
object.keys javascript
object.keys foreach
javascript array
javascript iterate object key, value
javascript object to array

I need to create an array based on some selection of keys inside an existing constant javascript object

const EXISTING_CONSTANT_OBJECT  = {
      'fr': '10',
      'es': '15'
      'us': '10'
      'uk': '7'
      //and so on for many other iso country codes and UNPREDICTABLE key names
}

I need to be able to create an array (without modifying EXISTING_CONSTANT_OBJECT for immutability reasons) with all keys whose value are equal to 10.

For example, the expected output is

object_to_create_arr = ["fr","us"]

I tried using reduce but failed.

note: I can use ES6 and usually prefer as it's usually more concise.


You can use Object.keys and filter

const obj = { 'fr': '10','es': '15','us': '10','uk': '7'}

let final = Object.keys(obj).filter(key => +obj[key] === 10)

console.log(final)

Array.prototype.keys(), You can use Object.keys and filter. const obj = { 'fr': '10','es': '15','us': '10','uk': '7'} let final = Object.keys(obj).filter(key => +obj[key] === 10)  Arrays are a special type of objects. The typeof operator in JavaScript returns "object" for arrays. But, JavaScript arrays are best described as arrays. Arrays use numbers to access its "elements". In this example, person[0] returns John:


You're on the right track with reduce. As mentioned with other answers, you can also use filter (which might actually be better).

Here is a solution using reduce

const obj  = {
  'fr': '10',
  'es': '15',
  'us': '10',
  'uk': '7'
}

let result = Object.keys(obj).reduce((acc, curr) => {
  if (parseInt(obj[curr], 10) === 10) {
    acc.push(curr)
  }
  return acc
}, [])

console.log(result)

Create an array based on the keys of a JavaScript object, Constructors are of two types in JavaScript i.e. built-in constructors(array and Constructors can be useful when we need a way to create an object “type” that  Notice how each item in the new object uses the array index of the value as the key (i.e. "0", "1", etc.). And you can now access each item in the new object using the conventional dot notation method (foo.bar). In the next section, we'll show you how to manually loop over an array to construct a new object. 2. Loop Over Array & Construct a New


Try this

function createArr(obj){
    let arr = [];
    for (let key in obj) if (obj.hasOwnProperty(key) && String(obj[key]) === '10') arr.push(key);
    return arr;
}

Object.keys( ) In JavaScript, Create an Array Iterator object, only containing the keys of the array, and then loop through each key: var fruits = ["Banana", "Orange", "Apple", "Mango"]; Associative Array in JavaScript Associative arrays are dynamic objects that the user redefines as needed. keys() method takes the object as an argument and returns the array with given object keys. Common pitfalls when working with Javascript Arrays In most programming languages, an array is a data structure represented by an arrangement of


Object.keys(<object>) will do the trick BTW: Your object is invalid. You're missing some commas.

const EXISTING_CONSTANT_OBJECT = {
  'fr': '10',
  'es': '15',
  'us': '10',
  'uk': '7'
  //and so on for many other iso country codes and UNPREDICTABLE key names
};

const keys = Object.keys(EXISTING_CONSTANT_OBJECT);
console.log(keys);

JavaScript Array keys() Method, Creates a new array based on whether the items of an array pass a Creates an array which contains arrays of key/value pairs of an object. Array; Plain objects also support similar methods, but the syntax is a bit different. Object.keys, values, entries. For plain objects, the following methods are available: Object.keys(obj) – returns an array of keys. Object.values(obj) – returns an array of values. Object.entries(obj) – returns an array of [key, value] pairs.


Object.keys() function returns the array of values corresponding to the keys of object you feed to this function. For example:

let yourObject = { key1: value1, key2: value2, key3:value3}
let arr = Object.keys(yourObject)
console.log(arr) // Output: [ 'key1', 'key2', 'key3']

Hope this helps!!

Useful Javascript Array and Object Methods, If we ever create a data structure of our own, we should implement them too. They are Object.entries(obj) – returns an array of [key, value] pairs. Remember, objects are a base of all complex structures in JavaScript. So we  Creating object with dynamic keys [duplicate] Ask Question How to use two arrays to create an object literal array in JavaScript. See more linked questions.


Object.keys, values, entries, keys() extracts keys from a given object, the returned array can be used as is, or couple it with a forEach loop. The Object. While it is a bit of an overkill for just sorting a single array, this prototype function allows to sort Javascript arrays by any key, in ascending or descending order, including nested keys, using dot syntax.


JavaScript: manipulating objects with Object.keys() – clubmate.fi, is sometimes completely confusing— or, at least, doesn't always make a whole​… But JavaScript still powers almost 95% of the 10 million most popular was no built-in method in JavaScript to easily access an object's property names, Object.keys() on an array-like object with random key ordering: @FelixKling Was just doing some research and that term popped up, made it seem like it was hard to do without it. – user6535476 Aug 24 '16 at 16:09


Object Keys, Values, and Entries: JavaScript ES6 Feature Series (Pt 9), Learn how to get keys of the object in JavaScript. The Object.keys() method returns an array of strings of a given object's own property/key names. Create a free Medium account to get Top Stories in your inbox. JavaScript  JavaScript : find an object in array based on object's property (and learn about the "find" function) Published on March 20, 2017 March 20, 2017 • 312 Likes • 48 Comments Report this post