2017-05-26 11 views
-3

オブジェクトがデータでいっぱいです。JSONオブジェクトを編集したい

このデータを追加、削除、上へ移動、下へ移動して編集します。私はこれを行う方法を発見した

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

:ここ

は私のオブジェクトの一例です。 - その技術的にあなただけの持っている

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.splice(index, 1); 

:これを使用する項目を削除するに

あなたの例のオブジェクト。

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; 

あなたが特定の操作を実行する必要がある場合は、あなたも同じように簡単にこれらの項目に割り当てることができます。

関連する問題