1

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.

2
  • 1
    use the method let arrayOfKeys = Object.keys( yourObject ) Commented Oct 2, 2019 at 19:46
  • 1
    @RicardoGonzalez not really, you did not fully read the question Commented Oct 2, 2019 at 19:55

5 Answers 5

8

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)

Here + is used for implicit conversion to number, because in strict equality '10' and 10 are not same

Sign up to request clarification or add additional context in comments.

Comments

0

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)

3 Comments

Thanks i really hesitated between your solution and @code maniac but his one is more concise and equally correct
no problem, they both achieve the same result! Just wanted to show how it can be done with reduce
Sure, appreciate it
0

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;
}

Comments

0

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);

If you want to just get the keys where the value is 10, it's just as simple as adding .filter

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

const keysWith10 = Object.keys(EXISTING_CONSTANT_OBJECT).filter(k => EXISTING_CONSTANT_OBJECT[k] === '10');
console.log(keysWith10);

Comments

0

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!!

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.