简体   繁体   中英

Javascript not recognizing array from variable

I have an array coming from an outside source and Javascript won't recognize it unless I copy and paste it from the log on back.

So, for example, the following code would output a string instead of an array for outside but if I copied the exact output from the log for outside and assign it manually to a new variable copyfromlog it will output as an array.

var outside = Drupal.settings.map.loc_json;

// Directly copied from output of previous
var copyfromlog = [
  [
    "<div class='test china'><h2>Fake School</h2><hr><p>Lorem Ipsum.</p></div>",
    54.345014,
    -1.265579,
    "school.png",
  ],
  [
    "<div class='test europe'><h2>Test School</h2><hr><p>Lorem Ipsum.</p></div>",
    53.345014,
    -6.265579,
    "school.png",
  ],
];

console.log(outside);

console.log(copyfromlog);

Why is one logging an array while the other logs a string? Why is this happening if the content is identical? 控制台日志

Eval was the solution.

var outside = eval(Drupal.settings.map.loc_json);

This treats the variable as an array instead of as a string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM