-1

I am trying to clone and update an object to store in array as new instance. but not works.

any one help me to sort this out?

here is my try:

 public getAppPropeties(locales):Observable<any>{

        const msgParms = [];

        locales.forEach((locale, i) => {

            this.messageParam['bd']['locale']=String(locale);

            msgParms[i] = Object.assign({}, this.messageParam);    ;

        });

        console.log( 'msgParms', msgParms ); //getting last value

}
user2024080
  • 1
  • 14
  • 56
  • 96
  • 1
    Your english, and therefore your question, is very unclear. – Carsten May 11 '18 at 06:24
  • I am trying to clone and update some value in an existing object, and strong in to array as new objects.. – user2024080 May 11 '18 at 06:27
  • Possible duplicate of [How to Deep clone in javascript](https://stackoverflow.com/questions/4459928/how-to-deep-clone-in-javascript) – JJJ May 11 '18 at 06:38
  • Use this.messageParam.bd.locale=String(locale) and msgParam.push(Object.assign({},this.messageParam)) – Eliseo May 11 '18 at 06:39
  • @Eliseo That does exactly the same thing as the OP's code. – JJJ May 11 '18 at 06:41
  • then, the problem is that this.messageParam.db is null or undefined – Eliseo May 11 '18 at 06:43
  • @Eliseo Then it would throw an error. The real problem is that they keep adding the same object to the array. – JJJ May 11 '18 at 06:44

1 Answers1

0

I tried like this:

msgParms[i] = Object.assign({}, this.messageParam, {"bd" : { "locale" : locale }} );

Works fine

user2024080
  • 1
  • 14
  • 56
  • 96