我们如何在角js中删除方括号[]和双引号“”?

时间:2022-09-15 16:05:55

I am using MEAN stack in my application with AngularJS as my front-end. How can I remove Square Brackets [] and Double quotes "" in repeated answer, My plunker actually we need show role vlaues in my app so we have used ng-repeat="mani in name.comments " to get the answer, and we got the answer like ["user"], ["admin"], ["kp"] , what we exactly looking we just need to show the values alone, not with the [] Square Brackets and Double quotes.... for example answer would be:- values are without in [] Square Brackets and Double quotes like 1. user , 2. admin, 3. kp

我在我的应用程序中使用MEAN堆栈,将AngularJS作为我的前端。如何在重复的答案中删除Square Brackets []和双引号“”我的plunker实际上我们需要在我的应用程序中显示角色值,所以我们使用ng-repeat =“mani in name.comments”来获得答案,我们得到了像[“user”],[“admin”],[“kp”]这样的答案,我们只需要单独显示值,而不是[] Square Brackets和Double quotes ....示例答案是: - 值不在[] Square Brackets和双引号中,如1. user,2。admin,3。kp

My Data:-

$scope.name = {
"_id": "5863d3aaacaeddc00663bc07",

"comments": [
{
"created": 1485511936350,
"role": [
"user"
],
"email": "selvam@e21designs.com",
"name": "selvam R",
"commentText": "mani selvam"
},
{
"created": 1485511936350,
"role": [
"admin"
],
"email": "selvam@e21designs.com",
"name": "selvam R",
"commentText": "mani selvam"
},
{
"created": 1485511936350,
"role": [
"kp"
],
"email": "selvam@e21designs.com",
"name": "selvam R",
"commentText": "mani selvam"
}
],
"created": "2016-12-28T15:00:58.777Z",
"isCurrentUserOwner": false
};

My Html:-

<div ng-repeat="mani in name.comments ">

                <td>{{$index + 1}}</td>
                <td>{{mani.role }}</td>

              </div>
  • Please look at into My plunker for reference ...

    请查看我的plunker以供参考......

  • Expecting answer would be without [] Square Brackets and Double quotes...

    期待答案是没有[] Square Brackets和Double引号......

  • This role are within the array so that only it's showing with [] Square Brackets and Double quotes , so how can we remove this , we need to show only values... we have tried many ways we know it's simple task but we unable to solve this issue..

    这个角色在数组中,所以只有它用[] Square Brackets和Double引号显示,所以我们如何删除它,我们只需要显示值...我们已经尝试了很多方法我们知道它是简单的任务但我们无法解决这个问题..

  • can we use css to remove this ?... any one knows the solution please help us... thanks

    我们可以用css来删除吗?...任何人都知道解决方案请帮助我们...谢谢

  • please update plunker as well to know the exact solution....

    请更新plunker以了解确切的解决方案....

4 个解决方案

#1


5  

You just need to select the first element of the array at index 0 using:

您只需要使用以下命令在索引0处选择数组的第一个元素:

{{ mani.role[0] }}

{{mani.role [0]}}

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
  $scope.name = {
    "_id": "5863d3aaacaeddc00663bc07",

    "comments": [{
      "created": 1485511936350,
      "role": [
        "user"
      ],
      "email": "selvam@e21designs.com",
      "name": "selvam R",
      "commentText": "mani selvam"
    }, {
      "created": 1485511936350,
      "role": [
        "admin"
      ],
      "email": "selvam@e21designs.com",
      "name": "selvam R",
      "commentText": "mani selvam"
    }, {
      "created": 1485511936350,
      "role": [
        "kp"
      ],
      "email": "selvam@e21designs.com",
      "name": "selvam R",
      "commentText": "mani selvam"
    }],
    "created": "2016-12-28T15:00:58.777Z",
    "isCurrentUserOwner": false
  };
});
<!DOCTYPE html>
<html ng-app="plunker">

<head>
  <meta charset="utf-8" />
  <title>AngularJS Plunker</title>
  <script>
    document.write('<base href="' + document.location + '" />');
  </script>
  <link rel="stylesheet" href="style.css" />
  <script data-require="angular.js@1.5.x" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.10/angular.min.js" data-semver="1.5.10"></script>
  <script src="app.js"></script>
</head>

<body ng-controller="MainCtrl">

  <div ng-repeat="mani in name.comments ">

    <td>{{$index + 1}}</td>
    <td>{{ mani.role[0] }}</td>

  </div>


</body>

</html>

#2


4  

You can use the toString() method to convert the array that is being displayed to a string. Like so:

您可以使用toString()方法将正在显示的数组转换为字符串。像这样:

        <td>{{mani.role.toString()}}</td>

That will display it as a string so the " and [] will be removed.

这会将其显示为字符串,以便删除“和[]。

Plunkr showing this.

Plunkr显示了这一点。

#3


0  

Your mani.role is an array, so do something like this:

你的mani.role是一个数组,所以做这样的事情:

<td><span ng-repeat="r in mani.role">{{ r }} </span></td>

#4


0  

Your data looks like Json data, so what you need is to read Json data through AngularJS by creating module like this:

您的数据看起来像Json数据,因此您需要通过创建如下模块来通过AngularJS读取Json数据:

var App = angular.module('App', []);

App.controller('Ctrl', function($scope, $http) {
  $http.get('data.json')
       .then(function(res){
          $scope.data = res.data;                
        });
});

Also, w3schools helpful in your case $http in AngularJS

另外,w3schools在你的案例中对AngularJS中的$ http有帮助

#1


5  

You just need to select the first element of the array at index 0 using:

您只需要使用以下命令在索引0处选择数组的第一个元素:

{{ mani.role[0] }}

{{mani.role [0]}}

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
  $scope.name = {
    "_id": "5863d3aaacaeddc00663bc07",

    "comments": [{
      "created": 1485511936350,
      "role": [
        "user"
      ],
      "email": "selvam@e21designs.com",
      "name": "selvam R",
      "commentText": "mani selvam"
    }, {
      "created": 1485511936350,
      "role": [
        "admin"
      ],
      "email": "selvam@e21designs.com",
      "name": "selvam R",
      "commentText": "mani selvam"
    }, {
      "created": 1485511936350,
      "role": [
        "kp"
      ],
      "email": "selvam@e21designs.com",
      "name": "selvam R",
      "commentText": "mani selvam"
    }],
    "created": "2016-12-28T15:00:58.777Z",
    "isCurrentUserOwner": false
  };
});
<!DOCTYPE html>
<html ng-app="plunker">

<head>
  <meta charset="utf-8" />
  <title>AngularJS Plunker</title>
  <script>
    document.write('<base href="' + document.location + '" />');
  </script>
  <link rel="stylesheet" href="style.css" />
  <script data-require="angular.js@1.5.x" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.10/angular.min.js" data-semver="1.5.10"></script>
  <script src="app.js"></script>
</head>

<body ng-controller="MainCtrl">

  <div ng-repeat="mani in name.comments ">

    <td>{{$index + 1}}</td>
    <td>{{ mani.role[0] }}</td>

  </div>


</body>

</html>

#2


4  

You can use the toString() method to convert the array that is being displayed to a string. Like so:

您可以使用toString()方法将正在显示的数组转换为字符串。像这样:

        <td>{{mani.role.toString()}}</td>

That will display it as a string so the " and [] will be removed.

这会将其显示为字符串,以便删除“和[]。

Plunkr showing this.

Plunkr显示了这一点。

#3


0  

Your mani.role is an array, so do something like this:

你的mani.role是一个数组,所以做这样的事情:

<td><span ng-repeat="r in mani.role">{{ r }} </span></td>

#4


0  

Your data looks like Json data, so what you need is to read Json data through AngularJS by creating module like this:

您的数据看起来像Json数据,因此您需要通过创建如下模块来通过AngularJS读取Json数据:

var App = angular.module('App', []);

App.controller('Ctrl', function($scope, $http) {
  $http.get('data.json')
       .then(function(res){
          $scope.data = res.data;                
        });
});

Also, w3schools helpful in your case $http in AngularJS

另外,w3schools在你的案例中对AngularJS中的$ http有帮助