0

I am trying to build an object, that takes in any value (preferrably an int) as the key, which generates the value for the key using a function, that has a dictionary for mapping out each individual digit to a digit of another language. Basically, I am passing a number in English as the key, and want to retrieve it's corresponding value of a different language.

For example, let there be an object numbers, whose only key will be dynamic, and that key will be transformed using a function to generate the value for that key in the object.

numbers = {
    [number]: convertNumber(number)
}


const convertNumber = (number) => {
     let dict = {
        1: '١',
        2: '٢',
        3: '٣',
        4: '٤',
        5: '٥',
        6: '٦',
        7: '٧',
        8: '٨',
        9: '٩'
    }
    let newNumber = '';
    number.toString().forEach(digit => {
        newNumber = newNumber+dict[digit];
    });
    return newNumber;
}

Expected Output: numbers[1234] = '١٢٣٤';

But this code does not work, and I searched online for variable key names but did not find a way to be able to use a dynamic key value to be able to transform it and obtain a different number as the value to it's key. Is there a way to accomplish this using ES6?

*EDIT 1: * The function is not the issue here, I had a few errors but would have definitely been able to fix it by myself, I want to be able to retrieve a value from an object based on the key itself.

10
  • What are you trying to achieve with map? There is no return inside of it. And whrere is your array to use map? Commented Sep 26, 2019 at 9:08
  • 1
    And since when is map a method of strings? Commented Sep 26, 2019 at 9:09
  • My bad, I wanted a way to loop through the string to generate a new number. I have modified it with forEach(). Commented Sep 26, 2019 at 9:10
  • 1
    @Tribunal: Strings don't have a forEach method either. Commented Sep 26, 2019 at 9:11
  • You can't use forEach either Commented Sep 26, 2019 at 9:11

3 Answers 3

2

First of all, I would recommend to set dict outside your function:

const dict = {
    1: "١",
    2: "٢",
    3: "٣",
    4: "٤",
    5: "٥",
    6: "٦",
    7: "٧",
    8: "٨",
    9: "٩"
  }

Then, as already has been told, you should split your string into characters to get an array of them, and then you can loop through that array. After that you should gather all of results to a string with join. And a function is as simple as next:

const convertNumber = number => {
    return number.toString().split("").map(digit => dict[digit]).join('');
};

Finally, I would recommend to use regular expressions to check if number is what it should be.

const dict = {
        1: "١",
        2: "٢",
        3: "٣",
        4: "٤",
        5: "٥",
        6: "٦",
        7: "٧",
        8: "٨",
        9: "٩"
      }
      
const convertNumber = number => {
        return number.toString().split("").map(digit => dict[digit]).join('');
    };

const someNumbers = [123, 345, 334, 885];

const getNumberObject = arr => {
    return arr.reduce((acc, cur) => {
        acc[cur] = convertNumber(cur)
        return acc;
    }, {})
}

const numbers = getNumberObject(someNumbers)

console.log(numbers)

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

7 Comments

How do I use this function to generate a value from the key passed? While getting the value of numbers[1234], I want the key to be 1234 (dynamic value passed down) and the value to be it's transformed value. (Obtained from dict using the convertNumber() function you have defined).
123: convertNumber(123) still has 123 as a static value. How do I get a dynamic value, which is passed as the key to this object that I can then pass in the convertNumber() function?
numbers[123] = convertNumber(123) . And instead of [123] can be any variable [anyVar]
const someNumbers = [123, 345, 334, 885]; is still static, with those 4 numbers. I want to have an object that takes in any key and returns it's value translated with convertNumber() function. The main purpose for this is that while there are fixed static string translations for words, like translation['word'] = 'translatedWord', for numbers, translation.numbers[any number] = translatedNumber, which will be done using convertNumber() function. Is there a way to do that?
You don't need an object, you need a function. You can't return object's value if there is no related key. Dynamically you can only assign key-value pair for object.
|
1

The problem in your code is you're not splitting your number string. If you just do number.toString(), it will be again a single string. So you need to split that string into individual digits and then check inside your dictionary. To split the string you can use number.toString().split('').

Please find working code below:

let convertNumber = (number) => {
  let dict = {
    1: '١',
    2: '٢',
    3: '٣',
    4: '٤',
    5: '٥',
    6: '٦',
    7: '٧',
    8: '٨',
    9: '٩'
  }
  let newNumber = '';
  number.toString().split('').forEach(digit => {
    newNumber = newNumber + dict[digit];
  });
  return newNumber;
}

console.log(convertNumber(1234))

Also, you can't make newNumber variable as const, because you're assigning a value to that variable.

1 Comment

You can create a snippet on SO, How to create a running snippet on so
1

You were not far from having something that works, however, you tried to map in a String which does not work.

To still be able to use map you can use split which will gives you an array of characters (ex: foo => ['f','o','o']) that you can use as you like. Then, to get a string back, you can use the join method on Array and you'll get your final value.

const convertNumber = number => {
  let dict = {
    1: "١",
    2: "٢",
    3: "٣",
    4: "٤",
    5: "٥",
    6: "٦",
    7: "٧",
    8: "٨",
    9: "٩"
  };
  const newNumber = number
    .toString()
    .split("")
    .map(digit => {
      return dict[digit];
    }).join('');
  return newNumber;
};

working example

7 Comments

If you explain what the OP did wrong and how/why your code works your answer will be much more helpful.
What about the main object? How do I accept a dynamic key and use the key to generate it's value?
digit is a dynamic key here. It could be anything (ex: dict[digit + 1] or dict[digit + "_suffix"] -- assuming it exists in dict ).
In numbers = { [number]: convertNumber(number) } where does number come from ?
It's not exactly a variable, I want it to be any string we pass as the key. For example, numbers[10] will have key as 10, and value as '١٢٣٤'.
|

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.