A
A
Azamat_TURBO2020-04-19 00:38:17
JavaScript
Azamat_TURBO, 2020-04-19 00:38:17

How to put the production of objects on stream?

There is a problem.

There is an array
let arr = [];
You need to constantly create a new object and insert it into the array.
let obj = {};
obj.word = 'qwerty';
arr.push(obj);

The problem is that when I insert an object multiple times, the array thinks that I am inserting the same object and updates the existing one.
The question is: how can you put the production of objects on a stream so that the newly created object is inserted as a new one, and not as an updated old one.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
H
hzzzzl, 2020-04-19
@hzzzzl

explain "permanently"

let arr = [];

for(i = 0; i < 1000; i++) {
  let obj = {};
  obj.word = 'qwerty: ' + Math.random();
  arr.push(obj);
}

console.log(arr)  // все разные

S
Somewhere Intech, 2020-04-19
@john36allTa

const Obj = function(word){
   this.word = word
}
let objects = [];
for (let i = 0; i < 10; i++)
   objects.push(new Obj(undefined))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question