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>

asked May 30, 2018 at 4:05

5 Answers 5

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);

answered May 30, 2018 at 4:10
Sign up to request clarification or add additional context in comments.

1 Comment

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.
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>

answered May 30, 2018 at 4:10

Comments

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 += ',';
}

answered May 30, 2018 at 4:11

Comments

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(','));

answered May 30, 2018 at 4:09

Comments

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());
answered May 30, 2018 at 4:26

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.