Javascript JSON.stringify() メソッドで配列を比較するサンプル

構文
JSON.stringify(配列1) === JSON.stringify(配列2)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
JSON.stringifyを使用して配列の要素をJSON文字列に変換して配列を比較します。
JSON.stringify() メソッドは、ある JavaScript のオブジェクトや値を JSON 文字列に変換します。
JSON.stringifyを使用して配列の要素をJSON文字列に変換して配列を比較します。 JSON.stringify() メソッドは、ある JavaScript のオブジェクトや値を JSON 文字列に変換します。
JSON.stringifyを使用して配列の要素をJSON文字列に変換して配列を比較します。
JSON.stringify() メソッドは、ある JavaScript のオブジェクトや値を JSON 文字列に変換します。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
const cftA = [12, 23, 34];
const cftB= [12, 23, 34];
const cftC = [34, 45, 66];
console.log( cftA === cftB);
console.log( JSON.stringify(cftA) === JSON.stringify(cftB) );
console.log( JSON.stringify(cftA) === JSON.stringify(cftC) );
const cftA = [12, 23, 34]; const cftB= [12, 23, 34]; const cftC = [34, 45, 66]; console.log( cftA === cftB); console.log( JSON.stringify(cftA) === JSON.stringify(cftB) ); console.log( JSON.stringify(cftA) === JSON.stringify(cftC) );
const cftA = [12, 23, 34];
const cftB= [12, 23, 34];
const cftC = [34, 45, 66];

console.log( cftA === cftB); 

console.log( JSON.stringify(cftA) === JSON.stringify(cftB) ); 
console.log( JSON.stringify(cftA) === JSON.stringify(cftC) );

実行結果
> false
> true
> false

JavaScript

Posted by arkgame