i wrote the following code .is there any one liner for the following ?
if (this.selectedUsersToAdd.length) {
this.selectedUsersToAdd.push(selectedUsersToAdd);
} else {
this.selectedUsersToAdd = selectedUsersToAdd;
}
i wrote the following code .is there any one liner for the following ?
if (this.selectedUsersToAdd.length) {
this.selectedUsersToAdd.push(selectedUsersToAdd);
} else {
this.selectedUsersToAdd = selectedUsersToAdd;
}
You can use ternary operator and assign value of array you want to add if first one is empty or result of concat if it's not empty.
let arr = [];
let add = [1, 2];
arr = arr.length ? arr.concat(add) : add;
console.log(arr)
In case you want to push whole new array to old one if old one is not empty, then first push new one and return old one.
let arr = [1];
let add = [1, 2];
arr = arr.length ? (arr.push(add), arr) : add;
console.log(arr)