Conditionally Add an Object to an Array in JavaScript
Nov 15, 2022
To conditionally add an object to an array in JavaScript, you can use if
in combination with push()
or concat()
.
const array = [{ name: 'Test' }, { name: 'John' }];
if (array.length < 5) {
array.push({ name: 'pass' });
}
let array2 = [{ name: 'Test' }, { name: 'John' }];
if (array2.length < 5) {
array2 = array2.concat([{ name: 'pass' }]);
}
Immutable Patterns with Spread Operator
If you are using immutable patterns, use the spread operator with the ternary operator.
let array2 = [{ name: 'Test' }, { name: 'John' }];
array2 = [
...array2,
// If `array2.length < 5`, add element to array. Otherwise add nothing.
...(array2.length < 5 ? [{ name: 'pass' }] : [])
];
Did you find this tutorial useful? Say thanks by starring our repo on GitHub!