-1
var obje = {hair: "yellow", eyes: "blue"};
var format = "hair + ' ' + eyes";
console.log(eval(format));

I try to make console.log(eval(format)) write "yellow eyes" to the console. How can I change from "hair" to "obje.hair" and from "eyes" to "obje.eyes" in the format string?

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
Emrah
  • 15
  • 1
  • Emrah, what exactly is the problem you're trying to solve here? Why do you _have_ to use `eval`? – Cerbrus Dec 19 '16 at 09:26
  • Problem is that if any format's word contains obje column name, this word can be changed like obje column name. For example, in format hair should be obje.hair. Because eval function accepts obje.hair not hair. – Emrah Dec 19 '16 at 09:31
  • Clearly this code is not representative of the problem – Jaromanda X Dec 19 '16 at 09:34
  • `obje[format.split(" ")[0]]; // yellow` `obje[format.split(" ")[1]]; // blue` – Flying Gambit Dec 19 '16 at 09:59

2 Answers2

3

Do not use eval.

Use something like this instead:

var obj = {hair: "yellow", eyes: "blue"};
var format = "{hair} {eyes}";

// Option 1: manual replacement
var result = format.replace('{hair}', obj.hair).replace('{eyes}', obj.eyes);
console.log(result);

// Option 2: automatic replacement
var result = format;
for(var key in obj){
  result = result.replace('{' + key + '}', obj[key]);
}
console.log(result);

Build a template string, then use string manipulation to replace the fields.

If you can use ES6, it allows for template literals out of the box:

var obj = {hair: "yellow", eyes: "blue"};
var result = `${obj.hair} ${obj.eyes}`;

console.log(result);
Community
  • 1
  • 1
Cerbrus
  • 70,800
  • 18
  • 132
  • 147
  • I know this is work but I must use eval function. How can I do ? – Emrah Dec 19 '16 at 09:20
  • Why _"must"_ you use `eval`, @Emrah? Why is that a requirement? – Cerbrus Dec 19 '16 at 09:21
  • 2
    If a question is _"How to drive into a tree without damaging my car"_, the only proper answer is: _"Don't drive into that tree"_, @JaromandaX. Using `eval` is like driving your car into a tree. – Cerbrus Dec 19 '16 at 09:24
  • @Emrah, can't you replace the `format` string with one of my examples? And if not, why not? – Cerbrus Dec 19 '16 at 09:40
  • I don't want to edit the format string manually and I want to use eval function. Is there any way to solve this problem ? – Emrah Dec 19 '16 at 09:43
  • @Emrah: Why do you insist on using `eval`? What is the string is supposed to be: `"Her hair color is {hair}"`? – Cerbrus Dec 19 '16 at 09:43
  • Now I try to your solution with replace just a minute – Emrah Dec 19 '16 at 09:44
  • I must use eval function because of this is the main question. – Emrah Dec 19 '16 at 09:50
  • You said before that that is your question, but ___why___? Why can't you change the format string to something proper? – Cerbrus Dec 19 '16 at 09:59
0

Use object.key to get as expected

var obje = {hair: "yellow", eyes: "blue"};
var format = "obje.hair + ' ' + obje.eyes";
console.log(eval(format));
Jyothi Babu Araja
  • 10,076
  • 3
  • 31
  • 38
  • I used object.key and format shows that "yellow + ' ' + blue" but this string doesn't work in eval function. Because of this reason, instead of yellow and blue, should be obje.hair and obje.eyes – Emrah Dec 19 '16 at 10:03
  • `object.key` means I used the generalised terms. In your case `object` is `obje` and `key` is `hair` or `eyes`. – Jyothi Babu Araja Dec 19 '16 at 10:23
  • I understand what you mean. If there is a hair in format, I should change obje.key. This solution may work. Can you give a code to change words like from hair to obje.key. Also, if I can do, I try this solution. – Emrah Dec 19 '16 at 10:35
  • @Emrah Sorry, can't get you clearly. What exactly you want? – Jyothi Babu Araja Dec 19 '16 at 11:15