if not exist then create array code example
Example: update object in array if id exists else append javascript
function upsert(array, item) {
const i = array.findIndex(_item => _item.id === item.id);
if (i > -1) array[i] = item;
else array.push(item);
}
const array = [
{id: 0, name: 'Apple', description: 'fruit'},
{id: 1, name: 'Banana', description: 'fruit'},
{id: 2, name: 'Tomato', description: 'vegetable'}
];
upsert(array, {id: 2, name: 'Tomato', description: 'fruit'})
console.log(array);
upsert(array, {id: 3, name: 'Cucumber', description: 'vegetable'})
console.log(array);