Home > OS >  How to add 2 String into array conditionally and print those value one down the other
How to add 2 String into array conditionally and print those value one down the other

Time:03-31

i have an array that is empty initially later put some conditions and print those items

const myItems= []
-----other code goes here
myItems = someCondition : "A B" : "A"

if someCondition is true should look like one or the other down

   "A"

   "B"

else

  "A"

CodePudding user response:

You could do use this:

var myItems = [];
let someCondition = Math.random() > 0.5;
myItems = someCondition ? "A B" : "A";

myItems.split(' ').forEach(e => console.log(e));

CodePudding user response:

you could simply do this :

const myItems = [];
myItems.push((condition ? "A B" : "A").splite(" "));

CodePudding user response:

Just push one of the expressions.

const items = [];

items.push(condition ? 'A B' : 'A');
  • Related