There is a code:

n = output.push(objects[0][i]) - 1; output[n].link = "/img/blank.gif"; 

When executed, it changes both output[n].link and objects[0][i].link

How can I avoid changing the variable objects[0][i].link ?

objects.slice()[0][i] did not help

ADDED: I push the elements of array A to array B. Then I change the newly added element of array B. This changes the element of array A, which I do not need. Question: How to avoid it?

Simply put, I want to return the changed array to the user without changing it on the server.

  • Here you are doing something very strange, it seems you generally do not understand what you are doing. I highly recommend reading some js tutorial for newbies before tackling a node. learn.javascript.ru say. Because the question shows a lack of understanding of the most basic principles of js. - Duck Learns to Take Cover
  • Describe in general the task itself better (what do you want to do with this code). - Duck Learns to Take Cover
  • I do not understand what you want to do? Lay out your array and what you want to do with it - Yuri
  • changed the question. - ivan0biwan
  • And why not to clone an array? Or do you need specific components? - Yuri

1 answer 1

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/assign

 n = output.push(Object.assign({}, objects[0][i])) - 1; output[n].link = "/img/blank.gif"; 
  • yes, it works, thank you very much - ivan0biwan
  • @Rundik, please note that it does not work just like that in any IE and it seems in safari. - Duck Learns to Take Cover
  • @ Duck Learning I use it in node.js - ivan0biwan
  • one
    @Rundik, well, in the node of the problem, if it will be only before the four, that is, in a very old one. - Duck Learns to Take Cover