0

In my below code Im am not able to fetch data within array

var str = "Service1|USER_ID, Service1|PASSWORD"
var str_array = str.split(',');
console.log(str_array)
for(var i = 0; i < str_array.length; i++)
{
    str_array[i] = str_array[i].split('|');
}
console.log(str_array)

This is the response from above code

 /*  [ [ 'Service1', 'USER_ID' ],
    [ 'Service1', 'PASSWORD' ] ]*/

I want response to be in two different array like below

var array1 = ['Service1']
var array2 = ['USER_ID','PASSWORD']

Any help on this will be really helpful

3
  • 2
    It's not a good idea to keep related data in two different arrays. Commented Apr 12, 2014 at 10:22
  • 1
    Do you mean you want the result to be something like {service1: ['user_id', 'password']} so you can do a result.service1? Commented Apr 12, 2014 at 10:32
  • @Pengtuzi Yes exactly. Commented Apr 12, 2014 at 10:35

3 Answers 3

3

Since you're on Node, you can do this:

var str = "Service1|USER_ID, Service1|PASSWORD";
var result = str.split(',').reduce(function(collected,splitByComma){

  var splitData = splitByComma.split('|');
  var key = splitData[0].replace(/\s+/gi,''); //Might want to improve this "trim"
  var data = splitData[1];


  if(!collected.hasOwnProperty(key)) collected[key] = [];
  collected[key].push(data);

  return collected;
},{});

console.log(JSON.stringify(result)); //{"Service1":["USER_ID","PASSWORD"]} 

//result.Service1[0] == USER_ID
//result.Service1[1] == PASSWORD

It's not wise to place stuff in separate places. You could have them under an object key though. If service name is variable, then you could do:

var serviceName = "Service1";
result[serviceName][0] == USER_ID
result[serviceName][1] == PASSWORD
Sign up to request clarification or add additional context in comments.

3 Comments

How can place the result in two different array as asked in my question [ [ 'Service1', 'USER_ID' ], [ ' Service1', 'PASSWORD' ] ]
var headerKeys = Object.keys(result); console.log(headerKeys) //Service1 console.log(result.headerKeys[0]) //undefined Why im getting undefined?
@user3180402 At the bottom of my answer, I added that.
0

As I have understand your question, you will want an array associated with each service key, to be able to do
services.service1 and get ['username', 'password' ] ? If so, here's a solution:

var str = "Service1|USER_ID, Service1|PASSWORD".replace(', ', ',').split(','), //[ 'Service1|USER_ID', 'Service1|PASSWORD' ]
   out = {};

   str.forEach(function(element){
    var key, value;
    element = element.split('|');
    key = element[0].trim();
    value = element[1].trim();
    out[key] = out[key] || []; // ensures we can push the value into an array
    out[key].push(value);
});

console.log(out); //{ Service1: [ 'USER_ID', 'PASSWORD' ] }

Comments

0

We can have a simple Regex solution

var res = "Service1|USER_ID, Service1|PASSWORD".split(/[\|,]/g);
var ar1 = [], ar2 = [];
res.forEach(function(em,i){
 if(i%2==0) {
  if(ar1.indexOf(em.trim())<0){
    ar1.push(em.trim());
  }
 } else {
  ar2.push(em.trim());
 }
});

//ar1 and ar2 will contain expected results

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.