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 3 years have passed since last update.

オブジェクトをStringに変換しkeyの「”」を外す

Posted at
sample.js

const obj = {
  obj1: "1111",
  obj2: "1111",
  obj3: "1111",
  obj4: {
    obj1: "1111",
    obj2: "1111",
  },
};

objToStr(obj);

function objToStr(sampleObj){
    const s_Json = JSON.stringify(sampleObj, null, 2);
    const line = s_Json.split('\n');

    line.forEach(function (val) {
        let keyAndValue = val.split(':');
        let left = keyAndValue[0].replace(/"/m, '');
        left = left.replace(/"/m, '');
        const right = keyAndValue[1] ? ':' + keyAndValue[1] : '';
        console.log(left + right + '\n');
    });
}
実行結果

{
  obj1: \"1111\",
  obj2: \"1111\",
  obj3: \"1111\",
  obj4: {
    obj1: \"1111\",
    obj2: \"1111\"
  }
}
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?