'Display different text values in an array with a button

Currently all values in the array are displayed at once when clicking on one of the two buttons. My goal is that the first value is displayed by default and with next/previous the next/previous array entry is displayed.

How can I realize realize this simply?

var array = ["text1", "text2", "text3"];

    function next(){
        //Next text in Array
        document.getElementById('text').innerHTML = array.join('<br />');
    }

    function prev(){
        //Previous text in Array
        document.getElementById('text').innerHTML = array.join('<br />');
    }
<div id="siggen">
    <div style="background-color: orange; height: 150px; width: 300px" id="content">
        <p id="text"></p>
    </div>
    <button id="btnPrev" onclick="prev()">Previous</button>
    <button id="btnNext" onclick="next()">Next</button>
</div>

Follow up Question

I looked at the link below from the answer. I have now added new datas to my array and would like to output the id in the left column and the name in the right column of my div.

The name is output correctly in the right column. But the id is not displayed but undefined is displayed. What is the reason for this?

The default values do not work yet either.

const arr =  [{_id:1,name:"T-Rex"},{_id:3,name:"Predator X"},{_id:4 ,name:"Velociraptor"},{_id: 6, name:"Triceratops"}]

    let currentIndex = 1;
    let currentName = arr[0].name;

    // initial log
    log(currentId)
    log(currentName);

    function next(){
        move();
    }

    function previous(){
        move(false);
    }

    function move(advance = true){
        currentIndex = (currentIndex + (advance ? 1 : -1) + arr.length) % arr.length;
        currentName = arr[currentIndex].name;
        currentId = arr[currentIndex].id;
        log();
    }

    function log(){
        document.getElementById('text').innerHTML = currentName;
        document.getElementById('id').innerHTML = currentId;
    }
<div style="background-color: orange; height: 300px; width: 500px" id="content">
    <div style="background-color: green; height: 100%; width: 250px; float: left">
        <p id="id"></p>
    </div>
    <div style="background-color: lightblue; height: 100%; width: 250px; float: right">
        <p id="text"></p>
    </div>
</div>
<button onclick="previous();">Previous</button>
<button onclick="next();">Next</button>


Solution 1:[1]

Something like this?

var array = ["text1", "text2", "text3"];


let currentIndex = 0;
let currentId = array[0];

log(currentId);

function next(){
  move();
}

function previous(){
  move(false);
}

function move(advance = true){
  currentIndex = (currentIndex + (advance ? 1 : -1) + array.length) % array.length;
  currentId = array[currentIndex];
  log(currentId);
}

function log(currentId){
document.getElementById('text').innerHTML = currentId;
}
<p id="text"></p>
<button onclick="next();">Next</button>
<button onclick="previous();">Previous</button>

Also, I got the code from this question, I only changed the code to make it display the text on the tag p

Solution 2:[2]

Extract digits from time column then convert them to quarter number. Finally, a simple groupby_sum do the job:

# Convert M1, M2, M3, M4, M5, M6 to Q1, Q1, Q1, Q2, Q2, Q2
to_quarter = df['time'].str[1:].astype(int).floordiv(4).add(1).astype(str).radd('Q')

out = df.assign(time=to_quarter).groupby(['company', 'metric', 'time']) \
                                .sum().reset_index()

Output:

>>> out
  company  metric time  data
0       x  X10384   Q1   300
1       y  X10456   Q2   600

Solution 3:[3]

Creating a dataframe based on your data:

data = {'Company' : ['x', 'x', 'x', 'y', 'y', 'y'],  
        'Metric' : ['X10384', 'X10384', 'X10384', 'X10456', 'X10456', 'X10456'],
        'time': ['M1', 'M2', 'M3', 'M4', 'M5', 'M6'],
        'data': [100, 100, 100, 200, 200, 200]}
df = pd.DataFrame(data)

Then create a dictionary and map it per time:

dict = {'M1': 'Q1', 'M2' : 'Q1', 'M3' : "Q1", 'M4' : 'Q2', 'M5' : 'Q2', 'M6' : 'Q2'}
df['time'] = df['time'].map(dict)

And groupby will give you the final result:

df.groupby(['Company','Metric','time']).sum().reset_index()

Solution 4:[4]

From months to quarters

input

company  metric  time   data
x        X10384  M1     100
x        X10384  M2     100
x        X10384  M3     100
y        X10456  M4     200
y        X10456  M5     200
y        X10456  M6     200

create a dictionary with a key in months for a value in quarters and map up column with key

months_to_quarters_dict = {'M1': 'Q1', 'M2' : 'Q1', 'M3' : "Q1", 'M4' : 'Q2', 'M5' : 'Q2', 'M6' : 'Q2'}
df['time'] = df['time'].map(months_to_quarters_dict)

output (1a)

company  metric  time   data
x        X10384  Q1     100
x        X10384  Q1     100
x        X10384  Q1     100
y        X10456  Q2     200
y        X10456  Q2     200
y        X10456  Q2     200

use a groupby().agg('sum') to get condensed df

df.groupby(['Company','Metric','time'], as_index=False).agg('sum')

output (1b)

company  metric  time   data
x        X10384  Q1     300
y        X10456  Q2     600

From quarters to months

input

company  metric  time   data
x        X10384  Q1     300
y        X10456  Q2     600

create a dictionary with a key in quarters for value in months and map up column with key

quarters_to_months_dict = {'Q1' : ['M1', 'M2', 'M3'], 'Q2' : ['M4', 'M5', 'M6']}
df['time'] = df['time'].map(months_to_quarters_dict)

output (2a)

  company  metric time  data
0       x  X10384   ['M1', 'M2', 'M3']   300
1       y  X10456   ['M4', 'M5', 'M6']   600

split rows using explode on time column and divide up data column by 3 to yield an equal amount for each month in a quarter

df = df.explode('time')
df['data'] = df['data].div(3)

output (2b)

company  metric  time   data
x        X10384  M1     100
x        X10384  M2     100
x        X10384  M3     100
y        X10456  M4     200
y        X10456  M5     200
y        X10456  M6     200

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Corralien
Solution 3 yakutsa
Solution 4 dreampopgaze