Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

get a part of a string in an array

get a part of a string in an array

Problem

I am currently using nodejs and I have a function which return a body (String) its like this :

 
    1328803684197
    1328803684289
                ....

So I want to get all the FIRST name part (without the quotes) from this string and put it in an array or a list (in javascript), like here I would like to have :

list[0]=AllAccounts
list[1]=Cascading_multi_select_report

I tried several way but it's not working, could you help me ? Thanks !

Problem courtesy of: Louis

Solution

Using xml2js:

var xml2js = require('xml2js'),
    parser = new xml2js.Parser();

parser.parseString(body, function(err, result) {
  var names = result.resourceDescriptor.map(function(resourceDescriptor) {
    return resourceDescriptor['@'].name;
  });
  console.log(names);
  // => [ 'AllAccounts', 'Cascading_multi_select_report' ]
});

Note: I'm not that familiar with xml2js, there might be better libraries out there, especially if you need to parse large documents.

Solution courtesy of: Linus Gustav Larsson Thiel

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

get a part of a string in an array

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×