简体   繁体   English

尝试将逗号分隔值的每个元素也作为数组元素推送

[英]trying to push each element with comma separated values also as an array element

I have the below json object. 我有下面的json对象。

$scope.myDataList = [
  {name:"one,two"},
  {name:"three"},
  {name:"four"},
  {name:"five,eight,nine"}
]

I'm iterating the list and push the elements in the array as shown below. 我正在迭代列表,并按如下所示将元素推入数组。

$scope.myArray = new Array();
angular.forEach($scope.myDataList, function (value, key) { 
  $scope.myArray.push(value.name); 
});

I want to push each element with comma spearated values also in the array. 我也想在数组中使用逗号分隔的值推送每个元素。 example: 例:
When I print $scope.myArray[0] , it prints one,two . 当我打印$scope.myArray[0] ,它打印one,two
But expected result is I want to print one for $scope.myArray[0] and two for $scope.myArray[1] , three for $scope.myArray[2] . 但是预期的结果是我想为$scope.myArray[0]打印one ,为$scope.myArray[1]打印two ,为$scope.myArray[2]三个。

Each value with comma separation also I want to push as an array element. 每个带有逗号分隔的值我也想作为数组元素推送。
I tried as below but does not work. 我尝试如下,但不起作用。

var array = $scope.myArray.split(',');

Demo: http://plnkr.co/edit/q36BQeqsj5GVNkP4PPhq?p=preview 演示: http//plnkr.co/edit/q36BQeqsj5GVNkP4PPhq?p = preview

You need to split the name and concatenate the result to the array instead: 您需要拆分name然后将结果连接到数组:

angular.forEach($scope.myDataList, function (value, key) { 
  $scope.myArray = $scope.myArray.concat(value.name.split(',')); 
});

Your problem has nothing to do with AngularJS , so I rewrote it in plain JavaScript: 您的问题与AngularJS无关,所以我用纯JavaScript重写了它:

 $scope = {}; $scope.myDataList = [{name:"one,two"},{name:"three"},{name:"four"},{name:"five,eight,nine"}] $scope.myArray = $scope.myDataList.map(({ name }) => name.split(',')) .reduce((result, splitElement) => result.concat(splitElement), []); console.log($scope.myArray); 

Note that the following line in your code 请注意,代码中的以下行

$scope.myArray.push(value.name);

pushes unsplit strings like 'five,eight,nine' into $scope.myArray . 将未拆分的字符串(如'five,eight,nine'推入$scope.myArray This is not what you want. 这不是您想要的。 Also, $scope.myArray.split(',') will fail, since Array.prototype has no split function. 同样, $scope.myArray.split(',')将失败,因为Array.prototype没有split功能。

One way to achieve the correct result is to map over the original array and split every element (the result is an array of string arrays). 获得正确结果的一种方法是映射到原始数组并拆分每个元素(结果是字符串数组的数组)。 After that, you can join the inner string arrays together using Array.concat . 之后,您可以使用Array.concat将内部字符串数组连接在一起。 This is what the following two lines do: 这是以下两行的内容:

$scope.myArray = $scope.myDataList.map(({ name }) => name.split(','))
     .reduce((result, splitElement) => result.concat(splitElement), []);

Here's a fairly simple means: 这是一个相当简单的方法:

$scope.myArray = $scope.myDataList.reduce((a, {name}) => a.concat(name.split(',')), [])

Note that what you're trying to do is take the orginal list and create a new one. 请注意,您要执行的操作是获取原始列表并创建一个新列表。 Since it's not a one-for-one mapping, it's not map . 由于它不是一对一的映射,因此不是map But reduce is appropriate, and carries more information than forEach . 但是reduce是合适的,并且比forEach携带更多的信息。

 const $scope = { myDataList: [ {name:"one,two"}, {name:"three"}, {name:"four"}, {name:"five,eight,nine"} ] } $scope.myArray = $scope.myDataList.reduce((a, {name}) => a.concat(name.split(',')), []) console.log($scope) 

while pushing the array element you can do: 在推送数组元素时,您可以执行以下操作:

 $scope.myArray = $scope.myArray.concat(value.name.split(",")); 

Here is the code to try. 这是尝试的代码。 http://plnkr.co/edit/nc91XI4vPJT0nEZvmMzU?p=preview http://plnkr.co/edit/nc91XI4vPJT0nEZvmMzU?p=preview

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

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