J
J
Jirafek2021-12-17 19:43:58
JavaScript
Jirafek, 2021-12-17 19:43:58

How to change the imported array?

There is a file with an array consisting of objects, I want to change it, I import it, but when I try to change it, I get an error that this is an import.
61bcbe4873ab1560186972.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Sokolov, 2021-12-17
@Jirafek

Option A.
Work with a copy.

import words from './words';

let badWords = words.slice(); 
badWords.push("Boo");
// ...
badWords = ['я передумал'];

Option B.
The array itself should remain the same, change only its elements:
import words from './words';

const myNewWords = ['иммутабельность', 'констистентность'];
words.splice(0, words.length); // удалили все элементы
words.push(...myNewWords);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question