简体   繁体   English

从带双引号的字符串到数组

[英]from strings with double quotes to array

"one"
"two"
"three"

Above is an output from ajax.以上是ajax的输出。 How can I convert them into an array?如何将它们转换为数组?

["one","two","three"]

I tried push() and split but the output is always ["one"]["two"]["three"]我试过 push() 和 split 但输出总是 ["one"]["two"]["three"]

 $(function() { $.ajax({ type: "GET", dataType: 'jsonp', url: 'https://spreadsheets.google.com/feeds/list/1VC633BXpMElJjRWvIRuZIP7UrEhuw6BdscnrV2heox0/1/public/full?alt=json', success: function(data) { var entry = data.feed.entry; //console.log(entry); for (var x in entry) { numbers = entry[x].gsx$numbers.$t; aaa = numbers.split(); console.log(aaa); } } }) });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Thanks for your help...谢谢你的帮助...

Explanation : [... ] converts the entry into an array.说明: [... ]entry转换为数组。 Using map function, returns all those strings .使用map函数,返回所有这些strings However, they may include empty strings .但是,它们可能包含empty strings That is why I have used filter , which filters out the strings with non-zero length, and assigns the resultant array to arr :)这就是我使用filter ,它过滤掉非零长度的strings ,并将结果数组分配给arr :)

 $(function() { $.ajax({ type: "GET", dataType: 'jsonp', url: 'https://spreadsheets.google.com/feeds/list/1VC633BXpMElJjRWvIRuZIP7UrEhuw6BdscnrV2heox0/1/public/full?alt=json', success: function(data) { var entry = data.feed.entry; var arr = [...entry].map((el)=>{ return el.gsx$numbers.$t; }).filter((el) => { return el.trim(); }); console.log(arr); } }) });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

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

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