简体   繁体   English

在javascript中针对逗号分割单个数组

[英]Split a single array with respect to comma in javascript

I am getting an array of single string $scope.Obj= ["Talking Spanish,Spanish food,Spanish things,Football"]; 我正在获取单个字符串$scope.Obj= ["Talking Spanish,Spanish food,Spanish things,Football"];的数组$scope.Obj= ["Talking Spanish,Spanish food,Spanish things,Football"];

The split should be by watching , 拆分应该通过观​​看,

I need to break it down = ["Talking Spanish","Spanish food","Spanish things","Football"]; 我需要将其分解为["Talking Spanish","Spanish food","Spanish things","Football"];

How can I do it using javascript? 我该如何使用JavaScript?

You can use split 您可以使用拆分

 let arr = ["Talking Spanish,Spanish food,Spanish things,Football"]; let op = arr[0].split(',') console.log(op) 

You can split 0th index of $scope.Obj with , and reassign to $scope.Obj. 您可以使用分割$ scope.Obj的第0个索引,然后将其重新分配给$ scope.Obj。

 $scope={}; $scope.Obj= ["Talking Spanish,Spanish food,Spanish things,Football"]; $scope.Obj=$scope.Obj[0].split(",") console.log($scope.Obj); 

The split() method splits a String object into an array of strings by separating the string into substrings, using a specified separator string to determine where to make each split. split()方法通过使用指定的分隔符字符串确定将每个字符串拆分为多个子字符串,将字符串对象拆分为多个子字符串,从而将String对象拆分为字符串数组。

In the following example, split() looks for spaces in a string and returns the first 3 splits that it finds. 在下面的示例中, split()在字符串中查找空格,并返回找到的前三个拆分。

var myString = 'Hello World. How are you doing?';
var splits = myString.split(' ', 3);

console.log(splits);

This script displays the following: 该脚本显示以下内容:

["Hello", "World.", "How"]

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

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