2017-03-07 60 views
0

再帰を使用してJSON.stringify()を複製しようとしています。返されたJSON文字列でなぜ定義されていないのか分かりません。ここで私はこれまで何をやったかだ:私は間違って行くよどこ再帰を使用してJSON文字列を作成する(文字列レプリケート)

var testobj = {num:123,str:"This is a string",bool:true,o:{x:1,y:2},iamnull:null} 
 
var count = 0 
 
var stringifyJSON = function(obj) { 
 

 
    // your code goes here 
 
    if(obj === undefined){ 
 
    \t return console.log("obj was undefined"); 
 
    } 
 
    count = count + 1 
 
    if(count > 20){ //I put this here mostly to stop infinite loops assuming this function wouldn't occur over 20 times. 
 
    \t return console.log("failed") 
 
    } 
 
    var endarray = []; 
 
if(obj !== undefined && typeof(obj)==='object'){ //If the object isn't undefined and the object is an object... 
 

 
    for(var prop in obj){ 
 
    \t console.log('"' + prop + '"'); 
 
    \t endarray.push(stringifyJSON(prop) +'"' + ':' +'"'+ stringifyJSON(obj[prop])) //push the items into the array, using recursion. 
 
    } 
 
    if(typeof(obj) !== undefined){ 
 
    \t return '{' + endarray.join() + '}' 
 
    } 
 
} 
 

 
    
 
    
 
}; 
 

 
//end result is "{undefined":"undefined,undefined":"undefined,undefined":"undefined,undefined":"{undefined":"undefined,undefined":"undefined},undefined":"{}}" 
 

 
//JSON values cannot be a function, a date, or undefined

誰かが示してもらえますか?再帰では、問題の原因を特定するのに問題があります。

+1

再帰を使用すると、何らかの結果を累積しようとしています。この場合、「エンド配列」。しかし、あなたは再帰の各反復でそれを空の配列にリセットしています。再帰関数の外側で 'endarray'を宣言し、完了するまで結果を構築する必要があります。 – rasmeister

+0

'typeof(obj)!== undefined'これは常に' true'です。 'undefined 'をチェックするのに' typeof'を使わないでください。それは、問題を引き起こすだけである、近視眼的で不必要なハックです。 –

+0

'stringifyJSON'を定義して2つのパラメータを受け入れることができます。' endarray'が定義されているかどうかを確認し、yesの場合は配列に値をプッシュし、そうでなければ 'endarray'を作成します。注意してくださいが、ネイティブの 'JSON.stringify()'実装では、オブジェクトまたは配列以上のものをパラメータとして受け入れることができます。 – guest271314

答えて

1

正しい解決策を得るためには、いくつかのことが必要です。

まず、再帰の基本ケースがないため、各再帰トレースのベースレベルで何も返されません(つまり、未定義が暗黙的に返されます)。最初に、int、文字列、ブール値、およびその他のプリミティブ型が文字列に変換されるベースケースを追加する必要があります。

typeof(null)は「オブジェクト」と評価されるので、再帰呼び出しの前にobj !== nullもチェックする必要があります。

var testobj = {num:123,str:"This is a string",bool:true,o:{x:1,y:2},iamnull:null} 
var count = 0 
var stringifyJSON = function(obj) { 

    // your code goes here 
    if(obj === undefined){ 
    return console.log("obj was undefined"); 
    } 
    count = count + 1 
    if(count > 20){ //I put this here mostly to stop infinite loops assuming this function wouldn't occur over 20 times. 
    return console.log("failed") 
    } 
    var endarray = []; 
if(obj !== undefined && obj !== null && typeof(obj)==='object'){ //If the object isn't undefined and the object is an object... 

    for(var prop in obj){ 
    console.log('"' + prop + '"'); 
    endarray.push(stringifyJSON(prop) +'"' + ':' +'"'+ stringifyJSON(obj[prop])) //push the items into the array, using recursion. 
    } 
    if(typeof(obj) !== undefined){ 
    return '{' + endarray.join() + '}' 
    } 
} 
if (obj !== undefined) {return String(obj)} 


}; 
+0

これは投げられたtestobjを返すので最も近い答えですが、引用符は答えの間違った場所にあります。 – Milos

+0

喜んで助けてください。私は引用の問題に気付きましたが、あなたのオリジナルの質問は '未定義'について尋ねました。うまくいけば、あなたは自分で引用符を理解することができますが、そうでなければ、より多くの助けを求めるために返信してください。 :) –

関連する問題