user7791702
user7791702

Reputation: 290

How to prevent a current value to be printed in array except others

How can I prevent the current value to be printed in array I mean if I have an array something like this

var fruits = ['Banana', 'Grapes', 'Apples', 'Orange'];

and I am looping out using for loop like thing :

fLen = fruits.length;
text = "<ul>";
for (i = 0; i < fLen; i++) {
    text += "<li>" + fruits[i] + "</li>";

    for (j = 0; j<fLen; j++) {
    text+=fruits[j];
    }
}

text += "</ul>";

The Output is:

Banana
Banana Grapes Apples Orange 
Grapes
Banana Grapes Apples Orange 
Apples
Banana Grapes Apples Orange 
Orange
Banana Grapes Apples Orange 

But,

What I want:

I want the output like thing

Banana
Grapes Apples Orange 
Grapes
Banana Apples Orange 
Apples
Banana Grapes Orange 
Orange
Banana Grapes Apples

means the current value should not be present in its second output

Upvotes: 0

Views: 81

Answers (7)

Yosvel Quintero
Yosvel Quintero

Reputation: 19070

You can do only one loop using Array.prototype.reduce():

var fruits = ['Banana', 'Grapes', 'Apples', 'Orange'],
    result = fruits.reduce((acc, val, ind, arr) => {
      acc += `<li>${val}</li>` + arr.join(' ').replace(val, '').trim();
      if (++ind === arr.length) {
        acc += '</ul>';
      }
      return acc;
    }, '<ul>');

document.getElementById('output').innerHTML = result;
li {list-style: none;}
<div id="output"></div>

Upvotes: 1

Frank Wisniewski
Frank Wisniewski

Reputation: 1224

why so complicated:

var fruits = ['Banana', 'Grapes', 'Apples', 'Orange'];
var text=`<ul>${fruits.map(e=>`<li>${e}</li>${fruits.filter(k => k!=e ? k : '')}`)}</ul>`.replace(/,/g, " ");
console.log(text);

Upvotes: 1

Yalamber
Yalamber

Reputation: 7580

Try something like below

var fruits = ['Banana', 'Grapes', 'Apples', 'Orange'];
var text = '<ul>';
fruits.forEach(function (element, index, array) {
  text += '<li>'+ element+'</br>';
  var newArray = array.map(function (element2, index2) {
    if(index != index2) {
      text += element2 + ' ';
    }
  });
  text +='</li>';
});
text += '</ul>';

Upvotes: 1

bharadhwaj
bharadhwaj

Reputation: 2129

Simply add an if condition which checks whether the index of both loops is same to the second for loop.

        var fruits = ['Banana', 'Grapes', 'Apples', 'Orange'];
        fLen = fruits.length;
        text = "<ul>";
        for (i = 0; i < fLen; i++) {
            text += "<li>" + fruits[i] + "</li>";

            for (j = 0; j < fLen; j++) {
              if (i !== j) {
                text += fruits[j] + ' ';
              }
            }
        }

        text += "</ul>";
        
        
        document.getElementById("element").innerHTML = text;
<div id="element"></div>

Upvotes: 2

Freddie
Freddie

Reputation: 738

var fruits = ['Banana', 'Grapes', 'Apples', 'Orange'];

var fLen = fruits.length;
var text = "<ul>";



fruits.forEach(function(fruit, i, array){
    text += "<li>" + fruit + "</li>";

    for (j = 0; j<fLen; j++) {

        if(j != i){
              text += fruits[j];
        }
    }
});

text += "</ul>";

Upvotes: 1

PotatoManager
PotatoManager

Reputation: 1775

duplicate the array for every iteration i.e. 4 times in a loop and pop consecutively different leader elements and then print the rest.

var fruits = ['Banana', 'Grapes', 'Apples', 'Orange'];
for (i=0;i<fruits.length;i++){
var temp=fruits;
var leader=temp.pop(fruits[i]);
//print leader
for(int j=0;j<temp.length;j++){
  //print temp[j]
}
}

Upvotes: 1

Amey Kumar Samala
Amey Kumar Samala

Reputation: 944

fLen = fruits.length;
text = "<ul>";
for (i = 0; i < fLen; i++) {
    text += "<li>" + fruits[i] + "</li>";

    for (j = 0; j<fLen; j++) {
        if (j != i) {
            text+=fruits[j];
        }
    }
}

text += "</ul>";

Just place a check to skip current index, which in your case is i

Upvotes: 2

Related Questions