2

Given an array of arrays with 2 vals such :

var arrayStd = [["G14",18],["G16",17],["G16",17],["G13",17]];

I currently use this or variations of this :

var arrayStd = [["G14",18],["G16",17],["G16",17],["G13",17]];

var arrayToString = function(array){ 
  var str;
  array.map(function(a) {
    var item = a[0]+":"+a[1];
    str = str? str+'; '+item : item;
    } 
  );
return str
}
var str = arrayToString(arrayStd);
console.log(JSON.stringify(str));

Or

var arrayStd = [["G14",18],["G16",17],["G16",17],["G13",17]];

var arrayToString = function(array){ 
  return str =  JSON.stringify(array).replace(/\"|\[\[|\]\]/g , "").replace(/\],\[/g , "; ").replace(/,/g , ":");
}
var str = arrayToString(arrayStd);
console.log(JSON.stringify(str));

Is there a slicker way to do obtain str= "G14:18; G16:17; G16:17; G13:17"

5
  • 3
    really no reason to use stringify, join("; ") will do it [["G14",18],["G16",17],["G16",17],["G13",17]].map(i=>i.join(':')).join('; ') Commented May 4, 2018 at 12:36
  • 4
    I'm voting to close this question as off-topic because it seems like a better fit for codereview.stackexchange.com Commented May 4, 2018 at 12:36
  • let str = arrayStd.map(e => e.join(':')).join(';'); Commented May 4, 2018 at 12:37
  • Seems return str = array.join("; ").replace(/,/g,":") would do :D Commented May 4, 2018 at 12:38
  • "Is there a slicker way to do" No. That's perfect. Commented May 4, 2018 at 12:39

5 Answers 5

4

You can use .map() and .join() like this:

let str = arrayStd.map(a => a.join(":")).join("; ");

Demo:

let arrayStd = [["G14", 18], ["G16", 17], ["G16", 17], ["G13", 17]];

let str = arrayStd.map(a => a.join(":")).join("; ");

console.log(str);

Sign up to request clarification or add additional context in comments.

1 Comment

Oh I see ! You enter deeper so you use map().join().join() and not join().replace() ! :D
2

Making incremental improvements to your code...

You should be using .forEach() instead of .map() since you're not returning anything.

var arrayStd = [
  ["G14", 18],
  ["G16", 17],
  ["G16", 17],
  ["G13", 17]
];

var arrayToString = function(array) {
  var str = "";
  array.forEach(function(a) {
    var item = a[0] + ":" + a[1];
    str = str ? str + '; ' + item : item;
  });
  return str
}
var str = arrayToString(arrayStd);
console.log(JSON.stringify(str));


However, .map() is actually useful here to map each inner array to a string, then take the new array of strings and join them.

var arrayStd = [
  ["G14", 18],
  ["G16", 17],
  ["G16", 17],
  ["G13", 17]
];

var arrayToString = function(array) {
  var str = array.map(function(a) {
    return a[0] + ":" + a[1];
  }).join("; ");

  return str
}
var str = arrayToString(arrayStd);
console.log(JSON.stringify(str));


You can also use .join() on the inner array, and can get rid of the str variable by returning the result immediately.

var arrayStd = [
  ["G14", 18],
  ["G16", 17],
  ["G16", 17],
  ["G13", 17]
];

var arrayToString = function(array) {
  return array.map(function(a) {
    return a.join(":");
  }).join("; ");
}
var str = arrayToString(arrayStd);
console.log(JSON.stringify(str));


Finally, in modern environments, you can convert your functions to "arrow functions" to get a more concise syntax.

const arrayStd = [
  ["G14", 18],
  ["G16", 17],
  ["G16", 17],
  ["G13", 17]
];

const arrayToString = array => array.map(a => a.join(":")).join("; ")

var str = arrayToString(arrayStd);
console.log(JSON.stringify(str));

I also changed var to const, which is a good idea for variables you won't modify.

Comments

1

Highly inspired by previous suggestions, yet shorter and more readable than others :

var arrayStd = [["G14",18],["G16",17],["G16",17],["G13",17]];

var arrayToString = function(array){ 
  return array.join("; ").replace(/,/g,":")
}
console.log(arrayToString(arrayStd));

1 Comment

People generally won't do this because it's performing all the same joins, but then needs to go back and create a whole new strings after a search and replace. Does work though, as along as you can guarantee that there will be no commas in the provided text.
0

You can use map function, and join the resulting array.

let arrayStd = [["G14",18],["G16",17],["G16",17],["G13",17]];
//"G14:18; G16:17; G16:17; G13:17"

let arrayNew = arrayStd.map((elem,i) => `${i!==0?'; ':''}${elem[0]}:${elem[1]}`).join("");
console.log(arrayNew);

Comments

0
let arrayStd = [["G14",18],["G16",17],["G16",17],["G13",17]];
//"G14:18; G16:17; G16:17; G13:17"

let arrayNew = arrayStd.map((elem,i) => `${i!==0?'; 
':''}${elem[0]}:${elem[1]}`).join("");
console.log(arrayNew);

2 Comments

Short but opaque.
The code is invalid. And this kind of template literal usage is very difficult to decipher.

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.