DEV Community

Lars Wächter
Lars Wächter

Posted on • Updated on

Dealing with lists in Javascript - Listing.js

Most of you might know lists from programming languages like Java or C#. At the company I'm working for we are using Adobe's ColdFusion on the backend. Here, we actually use lists quite often. In my view it's a nice alternative to Arrays. That's why I started working on a small JS library for dealing with lists in Javascript, called: Listing.js. At first the lib was intended only for private usage but I think there's no reason to not share it.

The library is available at GitHub

The methods from this library is a collection of list and array functions from other programming languages, so you will know the most of them. You can use this library in your browser and Node.js. Next, I want to give you a short introduction to the lib.

Valid delimiters and lists:

Listings.js recognizes three different types of delimiters:

var list = "1,2,3,4";
var list = "1;2;3;4";
var list = "1:2:3:4";
Enter fullscreen mode Exit fullscreen mode

Obviously, each number is a list element. The default list delimiter is the comma (","). Besides numbers, you can also use strings as list elements.

var list = "hello,I,am,Lars";
var list = "Nice;to;see;you";
Enter fullscreen mode Exit fullscreen mode

Note: Listing.js recognizes the empty spaces in lists. So if an element includes empty spaces and you try to find the element, make sure that your search parameter also includes the empty spaces.

var list = "hello my,name is,not Peter";
listing.find(list, 'name is');

// Result: 1
Enter fullscreen mode Exit fullscreen mode

Examples:

As next, here are some methods examples. You can find an overview of all methods and test cases for them at the GitHub Repo.

var list = "1,54,2,64,24,76";
listing.append(list, 89);

// Result: "1,54,2,64,24,76,89"
Enter fullscreen mode Exit fullscreen mode
var list = "1,54,2,64,24,76";
listing.changeDelims(list, ";");

// Result: "1;54;2;64;24;76;89"
Enter fullscreen mode Exit fullscreen mode
var list1 = "1,54,2";
var list2 = "77,31,501";;
listing.concat(list1, list2);

// Result: "1,54,2,77,31,501"
Enter fullscreen mode Exit fullscreen mode
var list = "ThisIs,aList,withStrings";
listing.contains(list, 'List');

// Result: "1"
Enter fullscreen mode Exit fullscreen mode
var list = "43,21,5,9,104";
listing.each(list, function(item, index) {
  // Iterartion 1 -> item = 43, index = 0
  // Iterartion 2 -> item = 21, index = 1
  // Iterartion 3 -> item = 5, index = 2
  // Iterartion 4 -> item = 9, index = 3
  // Iterartion 5 -> item = 104, index = 4
});
Enter fullscreen mode Exit fullscreen mode
var list = "13,32,54,13,11,6,103,54";
listing.getDuplicates(list);

// Result: "13,54"
Enter fullscreen mode Exit fullscreen mode
var list = "13,32,54";
listing.len(list);

// Result: "3"
Enter fullscreen mode Exit fullscreen mode
var myString = 'This is a test';
var list1 = 'a,test';
var list2 = 'no,fun';

listing.replace(myString, list1, list2);

// Result: "This is no fun"
Enter fullscreen mode Exit fullscreen mode

Let me know if you know more features you like to see in the lib. ;)

Top comments (6)

Collapse
 
jake_thurman profile image
Jake Thurman

I don't mean to be rude by this question, but why would anyone use this instead of arrays? It's just implementing arrays with strings. Which if storage is the concern, JSON is definitely a better, more standardized medium. Is there something I'm missing?

Collapse
 
lluismf profile image
Lluís Josep Martínez

The real WTF for me is a programming language that has no lists. Or queues. Or stacks. Or ...

Collapse
 
jake_thurman profile image
Jake Thurman

The built in JS array type can be used as a stack by using the built in .push() and .pop() methods. For a queue, you just push to index 0. No problem there if you ask me.

Also if you want more support there are countless libraries out there that cover other data structures.... Without using strings!

Thread Thread
 
lluismf profile image
Lluís Josep Martínez

But it's still an array, not a stack or a queue. You can't modify random elements in these structures.

Collapse
 
fiedsch profile image
Andreas Fieger

A string list might for exampel originate from user input who can't enter arrays in form fields.

But I'm with you. I'd split the string into an array and the work on the array.

Collapse
 
marluanespiritusanto profile image
Marluan Espirituanto

Excellent initiative :)