1

Can you pleas take a look at this demo and let me know how I can append , between elements of an array from the current output of

p[data-st!=all]p[data-st!=men]p[data-st!=bi]

To

p[data-st!=all], p[data-st!=men], p[data-st!=bi]

var filters = ['all',null,'men','bi',null];
var text = '';
var i;
for (i = 0; i < filters.length; i++) { 
if(filters[i]!= null){
  text += "p[data-st!="+filters[i]+"]";
}
    
}
console.log(text);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Behseini
  • 6,066
  • 23
  • 78
  • 125

5 Answers5

1

var filters = ['all',null,'men','bi',null];
var text = '';
var i;
for (i = 0; i < filters.length; i++) { 
if(filters[i]!= null){
  text += (i===0?"":", ") + "p[data-st!="+filters[i]+"]";
}
    
}
console.log(text);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Here's anotehr solution as well. You coul've pushed each p bracket to an array and join them. If you're not too worried about memory and iterations. Hear's a clean and easy to read solution. If you're not already familiar, it'll introduce you to two useful array methods.

function parseString(word)
{
  return `p[data-st!=${word}]`
}

var filters = ['all',null,'men','bi',null];
let text = filters
.filter(val => !!val)
.map(val => parseString(val))
.join(',');

console.log(text);
Facundo Larrosa
  • 3,349
  • 2
  • 14
  • 22
  • Just a note, the `!!` cast on `filter` is unnecessary. That aside, kinda strange to edit your answer 10 minutes after mine to include a near line-by-line duplicate... but I suppose your use of template literals could be a learning point for OP as well. – Tyler Roper May 30 '18 at 04:47
1

You could complete this (and clean up a bit) using array methods.

var filters = ['all',null,'men','bi',null];

var result = filters
                 .filter(n=>n)
                 .map(n => "p[data-st!="+n+"]")
                 .join(", ");

console.log(result);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Tyler Roper
  • 21,445
  • 6
  • 33
  • 56
0

Create a new array variable and push the modified by adding p[data-st! text to it and then use join method with comma(,) as delimiter to join the elements

var filters = ['all', null, 'men', 'bi', null];
var inArray = []
var i;
filters.forEach(function(item) {
  if (item !== null) {
    inArray.push("p[data-st!=" + item + "]")
  }
})
console.log(inArray.join(','));
brk
  • 48,835
  • 10
  • 56
  • 78
0

You can do a simple for loop over the string and add a coma after every right facing bracket. Not the best option but it'll work.

var newString = '';
for(let i = 0; i < text.length; i++)
{
  let char = text[i];
  newString += char;
  if(char === ']') newString += ',';
}
Eddie D
  • 1,120
  • 7
  • 16
0
var filters = ['all',null,'men','bi',null];
var a = [];
for(i in filters){
 if(filters[i]!= null){
  var b = [];
  b = "p[data-st!="+filters[i]+"]";
  a.push(b); 
 }
}
console.log(a.join());
malik kurosaki
  • 1,747
  • 15
  • 9