A Conditional Element Inside an Array(...) Construct

A conditional element inside an array(...) construct

Eventually I came up with the following:

callThatFunction(
array(k1 => v1, k2 => v2, ... kn=vn)
+ ($cond ? array(key=>value) : array())
)

Will still appreciate a suggestion for somethings that will express the intention more directly

How to define an array with conditional elements?

You can spread an array inside of an array, in order to keep items array clean, when the condition is false.

Here's how you can do it:

// Will result in ['foo', 'bar']const items = [  'foo',  ... true ? ['bar'] : [],  ... false ? ['falsy'] : [],]
console.log(items)

Adding an object conditionally inside an array

From what I understood, you want to filter away all the elements of the array given a condition. What I would do is adding a new key to the object specifying if it should be displayed, and then filter & map.

const typesOfCards = [
{ name: "Card A", size: "Medium", action: "make", type: "typeA" },
...
];
return typesOfCards.filter(card => card.type === "typeA").map(({ name, size, action }) => (
<BuildCard name={name} size={size} action={action} />
));

Add elements inside Array conditionally in JavaScript

When you spread into an array, you call the Symbol.iterator method on the object. && evaluates to the first falsey value (or the last truthy value, if all are truthy), so

let arr2 = ['value2', ...(condition && arr)];

results in

let arr2 = ['value2', ...(false)];

But false does not have a Symbol.iterator method.

You could use the conditional operator instead, and spread an empty array if the condition is false:

let condition = false;let arr1 = ['value1'];let arr2 = ['value2', ...(condition ? arr1 : [])];console.log(arr2);

Add conditional array elements

There are number of ways to do this.

1. Using spread operator (...)

var addThree=false;const array = [  "One",  "Two",  ...(addThree ? ['Three'] : []),]console.log(array);

How to define an array with conditional elements in react

It would be better to do the conditional operation outside the array definition. You can achieve this by conditionally doing push operation

if(props.id === undefined){
tableFilterFields.push({
label: "عنوان شخص",
name: "IdentityTitle",
type: FilterControls.Input,
},
{
label: "کد بورسی",
name: "AccountCode",
type: FilterControls.Input,
},
{
label: "نماد ",
name: "InstrumentPersianCode",
type: FilterControls.Input,
},
{
label: "نوع معامله ",
name: "TradeSideTitle",
type: FilterControls.Input,
})
} else {
tableFilterFileds.push( {
label: "نماد ",
name: "InstrumentPersianCode",
type: FilterControls.Input,
},
{
label: "نوع معامله ",
name: "TradeSideTitle",
type: FilterControls.Input,
})
}

Construct a Ruby array with conditional logic inside

Given that your b is actually a model attribute which may or may not be supported by the current self, then b is not a variable at all, b is a method call and you're not interested in whether or not the "variable" exists, you're interested in whether or not self responds to b. Hence you want to use respond_to?:

arr = [a, respond_to?(:b) ? b : nil, c]

or perhaps:

arr = [a, respond_to?(:b, true) ? b : nil, c]

if you want to allow b to be a non-public method.

defined?(b) should also work in this case (unless b's accessor method is automatically created via method_missing) but it will be rather puzzling to anyone looking at your code. Using respond_to? to see if an object has a method/attribute would be more idiomatic.

Using respond_to? when faced with method_missing of course assumes that both method_missing and respond_to? have been overridden but that should be a relatively safe assumption.

Conditional rendering of object INSIDE array

You could use the filter method:

var audi = false;
var cars= [
{header: 'fiat',accessor: row},
{header: 'audi',accessor: row},
{header: 'bmw',accessor: row}
].filter(car => audi || car.header !== 'audi')

Or spread operator:

var audi = false;
var cars= [
{header: 'fiat',accessor: row},
...(audi ? [
{header: 'audi',accessor: row}
] : []),
{header: 'bmw',accessor: row}
]

conditionally adding a element in a array

$a = array('a' => 'abc') + ($condition ? array('b' => 'xyz') : array());


Related Topics



Leave a reply



Submit