0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Javascript で object の中身を出力表示(Node.js 利用でコマンドライン)

Last updated at Posted at 2016-07-05

こんにちは。
Javascript で object の中身を出力表示させ、JSON.stringify の出力と比較してみました。Node.js 利用でコマンドラインで動かしました1

$ ./print.js 
{"0":["1","2"],"3":["4"]}  // JSON.stringify(比較用)
{0:[1,2],3:[4]}  // print_object()
print.js
# !/usr/bin/env node

function tostring(x, braces) {
  var arr = [];
  if (x instanceof Array) {
    if (braces === undefined) braces = "[]";
    arr.push(braces[0]);
    for (var i=0; i < x.length; i++) {
      if (i>0) arr.push(",");
      arr.push(tostring(x[i]));
    }
    arr.push(braces[1]);
    return arr.join("");
  }
  else if (x instanceof Object) {
    for(var k in x){
      arr.push(k + ":" + tostring(x[k]));
    }
    return tostring(arr, "{}")
  }
  else return x.toString();
}

function print_object(x) {print(tostring(x));}

function print(str) {process.stdout.write(str + '\n')};

var obj = {"0": ["1", "2"], "3": ["4"]};
print(JSON.stringify(obj));
print_object(obj);

  1. node.jsによるコマンドラインツール 導入

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?