如何保留数组中值的引号

时间:2016-10-14 15:40:01

标签: javascript arrays string stringify

我必须为一个类重做JSON.stringify的功能而且我被卡住了......

如果我传递了一个数组,我需要将确切的数组作为字符串返回。但是,当我返回数组时,它会删除任何字符串值的引号。例如:

var arr = [8, "hello"];
var addQuotes = function(arr){
            return ('[' + arr + ']');
        }
addQuotes(arr);
//"[8, hello]"

但是,我需要它返回:

"[8, "hello"]"

如何在数组值上保留引号?

1 个答案:

答案 0 :(得分:0)

我建议这段代码:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
<body>
<div class="article">
  <div class="content">
   Content 1
  </div>
  <button class="see-more">
  See more
  </button>
</div><div class="article">
  <div class="content">
   Content 2
  </div>
  <button class="see-more">
  See more
  </button>
</div><div class="article">
  <div class="content">
   Content 3
  </div>
  <button class="see-more">
  See more
  </button>
</div><div class="article">
  <div class="content">
   Content 4
  </div>
  <button class="see-more">
  See more
  </button>
</div><div class="article">
  <div class="content">
   Content 5
  </div>
  <button class="see-more">
  See more
  </button>
</div>
<div class="article">
  <div class="content">
   Content 6
  </div>
  <button class="see-more">
  See more
  </button>
</div>


</body>
</html>