2017-05-26 59 views
-3

我有一個對象填充數據。我想編輯我的JSON對象

我要添加,刪除,移動和下移以及編輯該數據。

這裏是我的對象的示例:

this.questions = 
{ 
    "testQS":[ 
    { 
    "quote": "Quote 888", 
    "type": "something", 
    }, { 
    "quote": "Quote 999", 
    "type": "something2, 
    } 
... 

我已經找到了如何做到這一點。

要刪除一個項目使用:

this.questions.testQS.splice(index, 1); 

要交換的元素中使用這樣的事情:

const temp = this.questions.testQS[index]; 
this.questions.testQS[index] = this.questions.testQS[index-1]; 
this.questions.testQS[index-1] = temp; 
+0

所以,我想在Typescript中編輯這個對象 –

+2

JavaScript對象(你有什麼)和它在JavaScript Object Notation(JSON,你沒有)中的字符串表示有區別。 – crashmstr

回答

0

JSON是一個對象的序列化字符串表示 - 所以在技術上你只是有一個在你的例子中的對象。

this.questions = { 
    testQS: [ 
     { quote: "Quote 888", type: "something" }, 
     { quote: "Quote 999", type: "something2" } 
    ] 
}; 

您可以訪問要素按下面的例子:

var allQuestions = this.questions.testQS; 
var firstQuestion = this.questions.testQS[0]; 
var secondQuestionQuote = this.questions.testQS[1].quote; 

如果你需要執行特定操作,還可以分配到這些項目很容易。