简体   繁体   English

我如何处理来自 java-script 的外部逗号

[英]How I can deal with outside commas from java-script

I'm facing a problem, flask app is returning an array in this form "['Aliens vs Predator: Requiem', 'Aliens', 'Anne of Green Gables']", but when I tried to access 0 index(first index), only [ this bracket prints on "demo" id;我遇到了一个问题,烧瓶应用程序以这种形式返回一个数组“['Aliens vs Predator: Requiem', 'Aliens', 'Anne of Green Gables']”,但是当我尝试访问 0 索引(第一个索引),只有 [ 这个括号打印在 "demo" id 上;

So I have to remove outside commas of arrays so that I will get this result: ['Aliens vs Predator: Requiem', 'Aliens', 'Anne of Green Gables'].所以我必须删除数组的外部逗号,这样我才能得到这个结果:['Aliens vs Predator: Requiem', 'Aliens', 'Anne of Green Gables']。 I didn't have any idea to do this, anybody can help me,我没有任何想法这样做,任何人都可以帮助我,

<!DOCTYPE html>
    <html>
    <body>
    
    <h2>Declaring an Array</h2>
    
    <p id="demo"></p>
    
    <script>
    
    var movies = "['Aliens vs Predator: Requiem', 'Aliens', 'Anne of Green Gables']";
    document.getElementById("demo").innerHTML = movies[0];
    
    </script>
    
    </body>
    </html>`

Technically there no bouble quotes on the string but still solution for your question maybe is mention below.从技术上讲,字符串上没有双引号,但下面可能会提到您的问题的解决方案。

 var movies = "['Aliens vs Predator: Requiem', 'Aliens', 'Anne of Green Gables']"; //will just remove start and last character from a string not from in between from a string const withoutFirstAndLast = movies.slice(1, -1); console.log(withoutFirstAndLast); //will replace all "" from a string const removed = movies.replaceAll('"', ''); console.log(removed);

You can parse your data this way (the JSON parser doesn't like single quotes):您可以通过这种方式解析数据(JSON 解析器不喜欢单引号):

 var movies = "['Aliens vs Predator: Requiem', 'Aliens', 'Anne of Green Gables']"; let data = JSON.parse(movies.replaceAll('\'',"\"")) console.log(data[0])

Just remove double quotes from "movies" variable.只需从“movies”变量中删除双引号。 it should look like this:它应该是这样的:

let movies = ['Aliens vs Predator: Requiem', 'Aliens', 'Anne of Green Gables'];

edit: my suggestion received minus (most likely from community bot) due to it is unclear.编辑:由于不清楚,我的建议收到了减号(很可能来自社区机器人)。 i do not agree, that it is unclear.我不同意,目前还不清楚。 because i don't know in what format array is received so i can't provide exact actions to be taken.因为我不知道收到的数组格式是什么,所以我无法提供要采取的确切行动。 if programmer is not able to print received array to check how it looks like he should quit programming.如果程序员无法打印接收到的数组来检查他应该退出编程的样子。 if he can, then it is not a big deal to search for function that could transform array into required format.如果他可以,那么搜索可以将数组转换为所需格式的函数并不是什么大问题。 i just pointed how array should look like to make things work properly in this situation我只是指出了数组在这种情况下应该如何使事情正常工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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