arnoldssss
arnoldssss

Reputation: 488

How to separate array of comma separated string into various arrays?

What's the best way to convert this array of comma separated values

[ 'com--test,LFutx9mQbTTyRo4A9Re5ksjdnfsI4cKN4q2,on',
  'com--fxtrimester,SEzlksdfMpW3FxkSbzL7eo5MmqkPczCl2,on',
  'com--fxtrimester,LFutx9mQbTTyRoldksfns4A9Re5I4cKN4q2,on' ]

Into three arrays?

[ 'com--test',           [ LFutx9mQbTTyRo4A9Re5Ilsdf4cKN4q2', ['on',
  'com--fxtrimester',      SEzMpW3FxkSbzL7eo5MmlkdfqkPczCl2',  'on',
  'com--fxtrimester' ]     LFutksdfx9mQbTTyRo4A9Re5I4cKN4q2 ]  'on']

I was trying something like:

 var indexToSplit = unique.indexOf(',');
 var status = unique.slice(3, indexToSplit - 1);
 var use = unique.slice(2, indexToSplit - 2);
 var pros = unique.slice(0, indexToSplit - 3);
 console.log(pros);

But I figured that is wrong ... any help is appreciated!

Upvotes: 0

Views: 83

Answers (3)

Rajesh
Rajesh

Reputation: 24915

You will have to loop over array and use string.split to get seperate parts.

Once you have seperate parts, you can push them to necessary array;

var d = [ 'com--test,LFutx9mQbTTyRo4A9Re5ksjdnfsI4cKN4q2,on',
  'com--fxtrimester,SEzlksdfMpW3FxkSbzL7eo5MmqkPczCl2,on',
  'com--fxtrimester,LFutx9mQbTTyRoldksfns4A9Re5I4cKN4q2,on'];

var result = [[],[],[]];
var len = 3;
d.forEach(function(str, i){
  var _tmp = str.split(',');
  for (var i = 0; i<len; i++){
    result[i].push(_tmp[i])
  }
})

console.log(result)


A little generic way.

  • Loop over data and split each string using comma(,)
  • Loop over split values and check if necessary array exists.
  • If not, initialise array, but you cannot do p[i] = [] as this will push to first value. You will have to also initialise all previous values. For this, you can use new Array(length). By default, if length is greater than 0, all indexes will be initialise to undefined.
  • Now push to necessary array. Position will be maintained.

var d = ['com--test,LFutx9mQbTTyRo4A9Re5ksjdnfsI4cKN4q2,on',
  'com--fxtrimester,SEzlksdfMpW3FxkSbzL7eo5MmqkPczCl2,on',
  'com--fxtrimester,LFutx9mQbTTyRoldksfns4A9Re5I4cKN4q2,on,test'
];


var result = d.reduce(function(p, c, index) {
  var _tmp = c.split(',');
  for (var i = 0; i < _tmp.length; i++) {
    // Check if position not defined.
    if (p[i] === undefined)
      // Initialize array and add default (undefined) to all elements before current element
      p[i] = new Array(index);

    p[i].push(_tmp[i])
  }
  return p;
}, [])

console.log(result)

Upvotes: 3

Sumit Gupta
Sumit Gupta

Reputation: 44

Since your question does not ask for a more general case, i am safely assuming it for 3 array. We can use forEach function on array below code can be one amongst the possible solutions

var arr1 = [];
var arr2 = [];
var arr3 = [];

var x = ['com--test,LFutx9mQbTTyRo4A9Re5ksjdnfsI4cKN4q2,on', 'com--fxtrimester,SEzlksdfMpW3FxkSbzL7eo5MmqkPczCl2,on', 'com--fxtrimester,LFutx9mQbTTyRoldksfns4A9Re5I4cKN4q2,on']

x.forEach(function(data) {
  var dataArray = data.split(',');
  arr1.push(dataArray[0]);
  arr2.push(dataArray[1]);
  arr3.push(dataArray[2]);
});


console.log(arr1)
console.log(arr2)
console.log(arr3)

Upvotes: 1

Steve Bennett
Steve Bennett

Reputation: 126105

With map this becomes:

  • for positions X out of 0, 1 and 2:
  • convert each item in the list into an array, and choose the Xth item

var start = [ 'com--test,LFutx9mQbTTyRo4A9Re5ksjdnfsI4cKN4q2,on',
  'com--fxtrimester,SEzlksdfMpW3FxkSbzL7eo5MmqkPczCl2,on',
  'com--fxtrimester,LFutx9mQbTTyRoldksfns4A9Re5I4cKN4q2,on' ]


var out = [0,1,2].map(i => 
  start.map(x => x.split(',')[i]) )
console.log(out)

Upvotes: 1

Related Questions