문제

I am trying to generate values dynamically and pass that to a function.

This is my code,

var color="{\"Hello\":\"#276040\"}";
alert(color); \\displays {"Hello":"#276040"}
sankey.setColors(color);

When I pass the color variable into that function it fails.

Instead of passing the argument, if I give the string directly it works.

sankey.setColors({"Hello":"#276040"}); //This works

What is the reason? How can I overcome that?

도움이 되었습니까?

해결책 3

make it as object. string is not a valid data for that

다른 팁

Assuming the dynamic parts of your parameters are the key name and value, you could do something like:

var keyName = 'Hello';
var keyValue = '#276040';
sankey.setColors( { keyName: keyValue} );

var color contains a string in your code, Be careful for what sankey.setColors need. "{\"Hello\":\"#276040\"}" and {"Hello":"#276040"} isn't same values.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top