`

How to convert Javascript Arrays to CSV (Comma separated values)

阅读更多

文章源自:http://viralpatel.net/blogs/convert-javascript-arrays-to-csv/

How to convert Javascript Arrays to CSV (Comma separated values)

Let’s see a small code snippet in Javascript that converts JS arrays in Comma separated values. It is also possible to convert an array to delimited text where one can choose the separator.

var fruits = ['apple', 'peaches', 'oranges', 'mangoes'];

var str = fruits.toString();

//print str: apple, peaches, oranges, mangoes

 

The toString() method converts an array into a String and returns the result. The returned string will separate the elements in the array with commas.

That’s simple. Javascript Array also has method valueOf() that does the same thing.

var fruits = ['apple', 'peaches', 'oranges', 'mangoes'];

var str = fruits.valueOf();

//print str: apple,peaches,oranges,mangoes

 

The valueOf() method returns the primitive value of an array. Again the returned string will separate the elements in the array with commas.

There is no difference in toString() and valueOf(). Try with different data types like number, strings etc it would give the same result.

Also sometimes one wants to create delimited string from an array. Lets say pipe (|) separated string!. In such cases Arrays join() method comes quite handy. The join() method joins the elements of an array into a string, and returns the string.

By default the join() method returns a comma (,) separated value of the array. But you can give argument to join() method and specify the separator. For example:

var fruits = ['apple', 'peaches', 'oranges', 'mangoes'];

var str = fruits.join("|");

//print str: apple|peaches|oranges|mangoes

 

How to convert CSV to Array in Javascript

Let us see how to convert a string with commas in it into an Array. Consider following case:

var fruits = ['apple', 'peaches', 'oranges', 'mangoes'];

var str = "apple, peaches, oranges, mangoes";

var fruitsArray = str.split(",");

//print fruitsArray[0]: apple

 Thus split() method of String comes handy for this. It is used to split a string into an array of substrings, and returns the new array. The split() method does not change the original string. Check the sytax of split() method.

string.split(separator,limit)

 

  • separator (Optional). Specifies the character, or the regular expression, to use for splitting the string. If omitted, the entire string will be returned (an array with only one item
  • limit (Optional). An integer that specifies the number of splits, items after the split limit will not be included in the array

Note: If an empty string (“”) is used as the separator, the string is split between each character. Thus:

var str="How are you doing today?";
var n=str.split("");

 The result of n will be an array with the values:

H,o,w, ,a,r,e, ,y,o,u, ,d,o,i,n,g, ,t,o,d,a,y,?

 

 

 

 

分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

    Effective JavaScript: 68 Specific Ways to Harness the Power of JavaScript[EPUB版]

    In order to truly master JavaScript, you need to learn how to work effectively with the language’s flexible, expressive features and how to avoid its pitfalls. No matter how long you’ve been writing...

    Think Java: How to Think Like a Computer Scientist [2016]

    Think Java: How to Think Like a Computer Scientist by Allen B. Downey, Chris Mayfield 2016 | ISBN: 1491929561 Currently used at many colleges, universities, and high schools, this hands-on ...

    JavaScript.Novice.to.Ninja.2nd.Edition

    Comprehensively updated to cover ECMAScript 6 and modern JavaScript development, the second edition of this step-by-step introduction to coding in JavaScript will show you how to to solve real-world ...

    A Smarter Way to Learn JavaScript.pdf

    《A Smarter Way to Learn JavaScript》是一本面向初学者的JavaScript学习书籍,它旨在通过使用技术手段来减少学习过程中的努力,帮助学习者快速掌握JavaScript的基本语法,并学会如何使用JavaScript操作HTML。...

    rgb2hex and hex2rgb Convert colors between rgb and hex values

    Simply convert between hex color values and rgb color values. These two functions can handle arrays as inputs. Default rgb values are scaled from 0 to 1 to match Matlab's syntax. However, if you'd ...

    A samart way to learn javascript

    《一种学习JavaScript的聪明方式》这本书的目录详细地梳理了学习JavaScript的基础知识和技巧,将教学内容分为多个章节,每个章节都专注于一个特定的主题或概念,使读者能按部就班地学习编程语言。以下是根据目录内容...

    Olympus-Introduction_to_Phased_Array

    标题“Olympus-Introduction_to_Phased_Array”指向了由 Olympus NDT 发布的一份详细介绍相控阵超声检测(Phased Array Ultrasonic Testing, PAUT)技术应用的手册。Olympus NDT 是一家全球领先的无损检测(Non-...

    java操作CSV文件

    在日常的数据处理工作中,CSV(Comma-Separated Values)文件是一种常见的数据交换格式。它简单易用且跨平台兼容性好,常被用于导入导出数据、数据分析等场景。在Java中,处理CSV文件有多种方式,本篇文章将详细介绍...

    Mastering.JavaScript.High.Performance.1784397296

    You will also learn about JavaScript performance on mobile platforms such as iOS and Android and how to deploy your JavaScript project to a device. Finally, by the end of the book, you'll be able to ...

    Introduction to Parallel Algorithms - Arrays Trees Hypercubes

    The contents itself is organized in three chapters according to the network architecture: arrays and trees for Chapter 1 (117 pages), meshes of trees for Chapter 2 (117 pages), and hypercubes and ...

    javacsv.jar.zip-jar包

    在Java编程中,CSV(Comma Separated Values)文件是一种常见的数据交换格式,用于存储表格数据,如电子表格或数据库。CSV文件以其简单性、易读性和跨平台兼容性而广泛使用。当涉及到在Java应用程序中读取、写入或...

    Mastering.JavaScript.1785281348

    The book will also teach you how to use arrays and objects as data structures. You will graduate from intermediate-level skills to advanced techniques as you come to understand crucial language ...

    Object-Oriented JavaScript(PACKT,2ed,2013)

    Look closely at numbers, Booleans and strings, and learn how arrays, loops and conditions help you to effectively manipulate and work with complex data using JavaScript. You’ll also find guidance ...

    java操作csv

    在Java编程中,CSV(Comma Separated Values)文件是一种常见的数据存储格式,它以逗号分隔每一列的数据,便于处理和交换表格类数据。本篇将详细讲解如何使用Java来读取、写入CSV文件,以及推荐的库和基本操作。 一...

    VC开发知识库文章:How to automate Excel from MFC and Visual C++

    How to automate Excel from MFC and Visual C++ 2005 or Visual C++ .NET to fill or obtain data in a range using arrays

    Simplifying [removed] Writing Modern JavaScript with ES5, ES6, and Beyond pdf

    Learn to write modern JavaScript not by memorizing a list of new syntax, but with practical examples of how syntax changes can make code more expressive. Starting from variable declarations that ...

    Mastering JavaScript(PACKT,2016)

    The book will also teach you how to use arrays and objects as data structures. You will graduate from intermediate-level skills to advanced techniques as you come to understand crucial language ...

    Mastering JavaScript [PACKT 2016]

    The book will also teach you how to use arrays and objects as data structures. You will graduate from intermediate-level skills to advanced techniques as you come to understand crucial language ...

Global site tag (gtag.js) - Google Analytics