Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

199
Views
I want the condition only to iterate through map function not the render part
    const generateTable = () => {
    let table = [];
    // Outer loop to create parent

    const rowValue = fetchedRowData;
    const columnsValue = fetchedColumnData;

    for (let i = 0; i < columnsValue; i++) {
      let children = [];
      //Inner loop to create children
      for (let j = 0; j < rowValue; j++) {

        children.push(
            
            products.map((item, index, k) =>{
                if (i+1 == item.placementColumn.value && j+1 == item.placementRow.value) { 
                    return(
                    <TouchableOpacity style={{width: 50, height: 50, marginBottom:10, marginLeft:5, backgroundColor: 'red', alignItems: "center", justifyContent: 'center'}} onPress={() => navigation.navigate('Internet', { paramKey: telephone, })}>
                        <Text style={styles.buttonText}>row: {j+1}</Text>
                        <Text style={styles.buttonText}>column: {i+1}</Text>
                    </TouchableOpacity>     )
                }
                else {
                    return(
                    <TouchableOpacity style={{width: 50, height: 50, marginBottom:10, marginLeft:5, backgroundColor: vmColor, alignItems: "center", justifyContent: 'center'}} onPress={() => navigation.navigate('Internet', { paramKey: telephone, })}>
                        <Text style={styles.buttonText}>row: {j+1}</Text>
                        <Text style={styles.buttonText}>column: {i+1}</Text>
                    </TouchableOpacity> )
                }
            })
            
            
        );
      }
      table.push(
        <View key={i}>
          <>{children}</>
        </View>
      );
    }
    return table;
};

Here are the key points to understand my problem:

  • I have generated a table in the above function, which takes rows and columns from the database and generate a table for every user.
  • I want to make a condition that if rows and columns value matches my desired value the colour of the box changes.
  • Note that my desired values are also in an array that needs to iterate to compare each value with column and row values (As you can see in above function)
  • The above function changes colour but it also iterates the row loop (in which array is being mapped) number of times the entities in the product array.
  • I just want to iterate condition through the loop so that each value in the array gets compared but not the jsx element to render multiple times
  • Any suggestion and solution will be appreciated.
  • Thanks in Advance :)
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If the only difference is some styling, you have two options:

  1. Conditional (ternary) operator
const generateTable = () => {
    let table = [];
    // Outer loop to create parent

    const rowValue = fetchedRowData;
    const columnsValue = fetchedColumnData;

    for (let i = 0; i < columnsValue; i++) {
        let children = [];
        //Inner loop to create children
        for (let j = 0; j < rowValue; j++) {
            children.push(
                products.map((item, index, k) => {
                    return (
                        <TouchableOpacity
                            style={{ width: 50, height: 50, marginBottom: 10, marginLeft: 5, backgroundColor: i + 1 == item.placementColumn.value && j + 1 == item.placementRow.value ? 'red' : vmColor, alignItems: "center", justifyContent: 'center' }}
                            onPress={() => navigation.navigate('Internet', { paramKey: telephone })}
                        >
                            <Text style={ styles.buttonText }> row: { j + 1 } </Text>
                            <Text style = { styles.buttonText } > column: { i + 1 } </Text>
                        </TouchableOpacity>
                    )
                })
            );
        }

        table.push(
            <View key={i}>
                { children }
            </View>
        );
    }
    return table;
};
  1. Adding multiple styles
const generateTable = () => {
    let table = [];
    // Outer loop to create parent

    const rowValue = fetchedRowData;
    const columnsValue = fetchedColumnData;
    let extraStyles;

    for (let i = 0; i < columnsValue; i++) {
        let children = [];
        //Inner loop to create children
        for (let j = 0; j < rowValue; j++) {
            children.push(
                products.map((item, index, k) => {
                    if (i + 1 == item.placementColumn.value && j + 1 == item.placementRow.value) {
                        extraStyles = { backgroundColor: 'red' };
                    } else {
                        extraStyles = { backgroundColor: vmColor };
                    }

                    return (
                        <TouchableOpacity
                            style={[{ width: 50, height: 50, marginBottom: 10, marginLeft: 5, alignItems: "center", justifyContent: 'center' }, extraStyles]}
                            onPress={() => navigation.navigate('Internet', { paramKey: telephone })}
                        >
                            <Text style={ styles.buttonText }> row: { j + 1 } </Text>
                            <Text style = { styles.buttonText } > column: { i + 1 } </Text>
                        </TouchableOpacity>
                    )
                })
            );
        }

        table.push(
            <View key={i}>
                { children }
            </View>
        );
    }
    return table;
};
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!