0

I have a "section" object that looks like below;

{
"my-field-1": {id: 'field-1', value: 1},
"my-field-2": {id: 'field-2', value: 2}
}

Now as soon as I execute;

section.fields = section

My section looks like below;

{
"fields": {
    "fields": {} //This repeats infinitely. How do I fix this ?
},
"my-field-1": {id: 'field-1', value: 1},
"my-field-2": {id: 'field-2', value: 2}
}

My question is what can I do to avoid the recurrsion inside the "fields". So I keep getting "fields" inside "fields" infinite times. All I need is finally section.fields should be an object with just those fields and without any recurssion; So, section.fields should just contain

{
"my-field-1": {id: 'field-1', value: 1},
"my-field-2": {id: 'field-2', value: 2}
}

2 Answers 2

1

i suggest to user assign funcion:

section =  {
"my-field-1": {id: 'field-1', value: 1},
"my-field-2": {id: 'field-2', value: 2}
}

section.fields = Object.assign({}, section);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot...I am using EmberJS, that also has an assign method...But I assume this is a normal JS method and should work fine even with Ember
1

You can make a shallow copy of object by using spread operator and then set it as property of object.

const section =  {
"my-field-1": {id: 'field-1', value: 1},
"my-field-2": {id: 'field-2', value: 2}
}

section.fields = {...section}
console.log(section)

1 Comment

Thanks...Only thing is this is throwing an Unexpected token JSHint error...but seems to be working otherwise if I try updating in the Chrome debug

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.