Updating Arrays & Objects

If you want to manipulate arrays. You must first declare that it's reactive using $state(value)

Adding an item to an array

<script>
  let sample = $state(['apple', 'peas']);
  sample.push('banana');
</script>

Adding an item to the begin of an array

<script>
  let sample = $state(['apple', 'peas']);
  sample.unshift('banana');
</script>

Deleting an item based on the name

<script>
  let list = $state(['apple', 'peas', 'bananas']);
  let filteredList = list.filter((item) => item !== 'apple');
</script>

Filtering an item based on the name

<script>
  let list = $state(['apple', 'peas', 'bananas']);
  let filteredList = list.filter((item) => item === 'apple');
</script>

Deleting first item in an array

<script>
  let sample = $state(['apple', 'peas']);
  let UpdatedSample = sample.slice(1);
</script>

Deleting last item in an array

<script>
  let sample = $state(['apple', 'peas']);
  let UpdatedSample = sample.slice(0, -1);
</script>

Last updated