chinds
chinds

Reputation: 1829

javascript, loop array, pull the 1st item only once

I have a a series of arrays I need to loop through and extract specific content.

here is an example of the arrays:

["Title", "Subline 1"]
["Title", "Subline 2"] 
["Title", "Subline 3"] 
["Title", "Subline 4"] 
["Title", "Subline 5"]

the title in each array will be the same but the subline is different.

I can loop thgouth this fine and pull either both elements or just the title or the subline.

But I would like to loop over the arrays pull the title of the first array then just the sub lines on the remaining arrays and this is where I get stuck, I have no idea to to structure a loop to do that.

The output should look like:

Title
Subline 1
Subline 2
Subline 3
Subline 4
Subline 5

Thanks in advance!

Upvotes: 0

Views: 1822

Answers (4)

Jaromanda X
Jaromanda X

Reputation: 1

If you have multiple titles ...

var arr1 = ["Title 1", "Subline 1"];
var arr2 = ["Title 1", "Subline 2"];
var arr3 = ["Title 1", "Subline 3"];
var arr4 = ["Title 2", "Subline 1"];
var arr5 = ["Title 2", "Subline 2"];

var arrays = [arr1, arr2, arr3, arr4, arr5];

var result = arrays.reduce(function(result, item) {
    var arr = result[item[0]] = result[item[0]] || [];
    arr.push(item[1]);
    return result;
}, {});

demo output to console

Object.keys(result).forEach(function(key) {
    console.log(key);
    result[key].forEach(function(sub) {
        console.log('    ', sub);
    });
});

output:

Title 1
    Subline 1
    Subline 2
    Subline 3
Title 2
    Subline 1
    Subline 2

Upvotes: 2

Khalav
Khalav

Reputation: 77

Create a variable to control if the title was already pulled out. Example:

var arr1 = ["Title", "Subtitle"];
var arr2 = ["Title", "Subtitle2"];
var arr3 = ["Title", "Subtitle3"];

var titlePulled = false;
var i = 0;

//1st for
for (i = 0; i < arr1.length; i++) {
    if (!titlePulled && i == 0) {
        titlePulled = true;
        console.log(arr1[i]);
    }
    else {
        console.log(arr1[i]);
    }
}

//2nd for
for (i = 0; i < arr2.length; i++) {
    if (!titlePulled && i == 0) {
        titlePulled = true;
        console.log(arr2[i]);
    }
    else {
        console.log(arr1[i]);
    }
}

And so on

Upvotes: 0

Barmar
Barmar

Reputation: 781059

You can access the title from the first element before the loop.

var array = [
    ["Title", "Subline 1"],
    ["Title", "Subline 2"],
    ["Title", "Subline 3"],
    ["Title", "Subline 4"],
    ["Title", "Subline 5"]
];

console.log(array[0][0]); // First title
for(var i = 0; i < array.length; i++) {
    console.log(array[i][1]); // Every Subline
}

Upvotes: 0

Yury Tarabanko
Yury Tarabanko

Reputation: 45121

Assuming you have an array of arrays named arrays

var result = [arrays[0][0]].concat(arrays.map(function(item){
  return item[1];
}));

Or if you want it to be "one loop"

var result = arrays.reduce(function(acc, x, i) {
    if(!i) acc.push(x[0])
    acc.push(x[1]);
    return acc;
}, []);

Upvotes: 4

Related Questions