1
var toFormat = [[1, "one", "unu"], [2, "two", "du"], [3, "three", "tri"], [4, "four", "kvar"]];

I need to output array toFormat so it should look like this:

1 (one)
2 (two)
3 (three)
4 (four)

(The third element of every sub-array is not used) How to do that?

EDIT: The loop is here

var res = []; 
for(var i=0;i<toFormat.length;i++){
    res.push(toFormat[i][1]+" ("+toFormat[i][2]+")");
}
console.log(res.join("\n"))
5
  • tried the loop but i dont really think it will work Commented Nov 21, 2016 at 20:19
  • 1
    Post your loop. Commented Nov 21, 2016 at 20:19
  • var res = []; for(var i=0;i<toFormat.length;i++){res.push(toFormat[i][1]+" ("+toFormat[i][2]+")");}console.log(res.join("\n")); Commented Nov 21, 2016 at 20:20
  • 1
    And the body of your for loop?, also edit your question with this not in a comment... Commented Nov 21, 2016 at 20:22
  • You are using 1 and 2 as the indexes and not 0 and 1. jsfiddle.net/5aqzkeox Commented Nov 21, 2016 at 20:27

3 Answers 3

5

const toFormat = [
   [1, "one",   "unu"], 
   [2, "two",   "du"], 
   [3, "three", "tri"], 
   [4, "four",  "kvar"]];

const result = toFormat.map(([val, string]) => `${val} (${string})`).join('\n');

console.log(result);

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

2 Comments

In this one liner solution there is quite good use of es6 features :)
Nice use of interpolation!
1

There are a ton of ways this can be done. I'd personally recommend using a for...of loop, like this

// defining the array to loop over
const toFormat = [
  [1, 'one', 'unu'],
  [2, 'two', 'du'],
  [3, 'three', 'tri'],
  [4, 'four', 'kvar']
];

for (let i of toFormat) {
  console.log(i[0] + " (" + i[1] + ")");
}

Of course, if you want this as a string instead of being printed you can do this instead

// defining the array to loop over
const toFormat = [
  [1, 'one', 'unu'],
  [2, 'two', 'du'],
  [3, 'three', 'tri'],
  [4, 'four', 'kvar']
];

let formatted = "";
for (let i of toFormat) {
  formatted += (i[0] + " (" + i[1] + ")\n");
}
console.log(formatted);

2 Comments

From MDN: It is strongly recommended that assignment operators (+, +=) are used instead of the concat() method.
@torazaburo Thanks for the advice! I did not know. I'll fix my answer to use + instead of concat.
0

First, you have omitted the closing quote of the last index in the last array.

But, barring that, the [Array.prototype.forEach()] method can do the trick:

var toFormat = [[1, "one", "unu"], [2, "two", "du"], [3, "three", "tri"], [4, "four", "kvar"]];
  
toFormat.forEach(function(item, index, arry){
    console.log(item[0] + " (" + item[1] + ")");
});
                                                                          

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.