add a person to window and push the person to an array in one line in javascript

To add a person to a window and push the person to an array in one line, you can use the following code:

index.tsx
const person = { name: "John", age: 30 };
const personsArr = ((w) => (w.persons = w.persons || []).concat(person))(window);
124 chars
3 lines

This code uses an immediately invoked function expression (IIFE) to add the person object to the window object and return the updated array of persons. If the persons array does not exist on the window object, it is created first. The concat method is used to append the new person object to the array.

Note that this code uses ES6 arrow functions and the functional programming style to create a concise and expressive one-liner.

gistlibby LogSnag