2017-04-25 62 views
0

我是AngularJS和JSON的新手。我被困在這個階段,我想在JSON中過濾不必要的字段。如何在控制器內格式化JSON數據

我在我的控制器使用的代碼:

var data = $scope.choices; // Is an array 
var datav = (JSON.stringify(data)); // array converted into a string need to be filtered 
alert(datav); 

如果我alert(datav)我得到這下面提到的JSON數據。

[{"title":"g","$$hashKey":"object:3"},{"id":"choice2","$$hashKey":"object:6","title":"ghgh"},{"id":"choice3","$$hashKey":"object:11","title":"fgh"}] 

我只是想 「稱號」 我不想$$hashKeyid。這個怎麼做?

+0

你嘗試過什麼到目前爲止?你有沒有試過https://docs.angularjs.org/guide/filter或https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter? –

+0

使用'.map()'只返回你想要的 –

回答

0

您可以使用Array.prototype.map所需的對象。

當您第一次收到您的陣列:

const data = [ 
    {title: "hello" , "unwanted" : 34}, 
    {title: "hello" , "unwanted" : 35}, 
    {title: "hello" , "unwanted" : 36}, 
    {title: "hello" , "unwanted" : 37}, 

] 



const wanted = data.map(d => { 
    return {title: d.title} 
}); 

console.log(wanted); 
+0

am開始日誌爲[undefined,undefined]。 –

+0

@PrashanthHarish我的錯誤,我犯了一個輕微的語法錯誤。看看這個[JSbin](http://jsbin.com/zazuzo/5/edit?js,console) –

+1

感謝Daniel Cooke它爲我工作... –

3

可以使用Array.map()函數來實現你想要什麼,並且只返回你有興趣

var data = [{"title":"g","$$hashKey":"object:3"},{"id":"choice2","$$hashKey":"object:6","title":"ghgh"},{"id":"choice3","$$hashKey":"object:11","title":"fgh"}]; 
 

 
var datav = data.map(d => ({title: d.title})); 
 

 
console.log(datav)

4

屬性可以使用angular.toJson代替JSON.stringify這將省去$$hashkey你。

angular.toJson

Serializes input into a JSON-formatted string. Properties with leading $$ characters will be stripped since AngularJS uses this notation internally.

這樣,

var myobj = [{ 
 
    "title": "g", 
 
    "$$hashKey": "object:3" 
 
}, { 
 
    "id": "choice2", 
 
    "$$hashKey": "object:6", 
 
    "title": "ghgh" 
 
}, { 
 
    "id": "choice3", 
 
    "$$hashKey": "object:11", 
 
    "title": "fgh" 
 
}] 
 

 
console.log(angular.toJson(myobj)) 
 
console.log(JSON.stringify(myobj))
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

編輯:如果你想只顯示某些屬性,使用Array.map在其他的答案中描述。 angular.toJson只會有幫助在這裏當你想省略只有$$hashkey保留一切。

+0

謝謝@tanmay。但我不想要id字段也... –

+0

@PrashanthHarish當然,我覺得問題有點誤導,會爲你編輯。你可以在這裏接受其他合適的答案 – tanmay

0
var data = $scope.choices.map(function(index,item){ 
    return {id: item.id}; 
}); 

這是你可以如何映射你需要選擇出數組的

+0

他要求'title',而不是'id' – rpadovani

+0

我給了他使用map函數的想法,他肯定可以解決他需要的東西。 –

0

我相信你使用的是第三方庫。我的猜測是Angular Material,它爲下拉數組值添加了類似的$$haskey屬性。理想情況下,這不應該對你的數組做任何改變,你應該仍然能夠獲得你的數組對象的屬性。

但是,如果您指定要刪除這些不需要的屬性,則應該使用.map函數從該數組中創建一個新陣列。例如:

var newArray= orginalArray.map(element => ({title: element.title})); 

您可以根據需要取出儘可能多的屬性並留下不需要的屬性。您的新陣列是與舊陣列完全不同的參考。

希望這有助於

1

試試這個

<html> 
 
<head> 
 
\t <script Src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js"></script> 
 

 
\t <script> 
 
\t \t var app=angular.module("myapp", []); 
 
\t \t app.controller("namesctrl", function($scope){ 
 
      var data = [{"title":"g","$$hashKey":"object:3"},{"id":"choice2","$$hashKey":"object:6","title":"ghgh"},{"id":"choice3","$$hashKey":"object:11","title":"fgh"}]; 
 
      var data1 = data.map(d => ({title: d.title})); 
 
      console.log(data1); 
 
\t \t }); 
 

 
\t \t 
 
\t </script> 
 

 

 
</head> 
 
<body ng-app="myapp" ng-controller="namesctrl"> 
 

 
</body> 
 
</html>

+0

TypeError:str.map不是函數。我得到錯誤.. –

+0

什麼是'str'?確保它是一個數組。 –

+0

我字符串數組存儲在str中的字符串.. –