Complete the code to create a new array with an added item immutably.
let items = [1, 2, 3]; let newItems = items[1]4);
Using .concat() returns a new array with the added item, keeping the original array unchanged.
Complete the code to update an object property immutably using spread syntax.
let user = { name: 'Alice', age: 25 };
let updatedUser = { ...user, age: [1] };Using user.age + 1 updates the age property by increasing it by one, creating a new object without changing the original.
Fix the error in the code to update an array item immutably.
let numbers = [10, 20, 30]; let newNumbers = numbers.map((num, index) => index === 1 ? [1] : num);
Using num + 5 updates the item at index 1 by adding 5 to its current value, keeping the update immutable.
Fill both blanks to immutably update a nested object property.
let state = { user: { name: 'Bob', age: 30 } };
let newState = { ...state, user: { ...state.user, [1]: [2] } };We update the age property to 31 immutably by spreading the existing user object and changing only the age.
Fill all three blanks to immutably filter and map an array in Svelte.
let todos = [
{ id: 1, done: false },
{ id: 2, done: true },
{ id: 3, done: false }
];
let activeTodoIds = todos
.filter(todo => todo.[1] === [2])
.map(todo => todo.[3]);This code filters todos that are not done (done === false) and then maps to get their id values, all done immutably.