Я пытаюсь создать динамический вложенный объект, но не могу изменить значение свойств, которые я создаю
TS
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
name = 'Test 1';
expectedResult = {
"0": { test: "Whoever" },
"1": { test: "Whoever" },
"2": { test: "Whoever" },
"3": { test: "Whoever" },
"4": { test: "Whoever" },
"5": { test: "Whoever" },
"6": { test: "Whoever" },
"7": { test: "Whoever" },
};
obj = <any>{};
test() {
for (var i = 0; i <= 7; i++) {
// how do i write this line
this.obj[i].test = "Whoever" //<-------------------------
}
}
}
HTML
<code><hello name="{{ name }}"></hello>
<button class="btn btn-sm btn-outline-primary" (click)="test()">
Test me</button>
<pre>
{{obj|json}}
{{expectedResult|json}}
Добавление стекаблица для справки.
https://stackblitz.com/edit/mavvtogotest
РЕДАКТИРОВАТЬ
Спасибо всем, кто помогает, (извините), возможно, я не объяснил это хорошо.Решение должно работать над несколькими вложенными объектами, например:
expectedResult = {
"0": {
"0": {
"0": { "test": "Whoever" },
"1": { "test": "Whoever" },
"2": { "test": "Whoever" }
},
"1": {
"0": { "test": "changeMe","notToBeChanged":"oldValue" },
"1": { "test": "changeMe","notToBeChanged":"oldValue" },
"2": { "test": "changeMe","notToBeChanged":"oldValue" }
},
"2": {
"0": { "test": "changeMe" ,"notToBeChanged":"oldValue" },
"1": { "test": "changeMe" ,"notToBeChanged":"oldValue" },
"2": { "test": "changeMe" ,"notToBeChanged":"oldValue" }
}
},
"1": {
"0": {
"0": { "test": "changeMe" ,"notToBeChanged":"oldValue" },
"1": { "test": "changeMe" ,"notToBeChanged":"oldValue" },
"2": { "test": "changeMe" ,"notToBeChanged":"oldValue" }
},
"1": {
"0": { "test": "changeMe" ,"notToBeChanged":"oldValue" },
"1": { "test": "changeMe" ,"notToBeChanged":"oldValue" },
"2": { "test": "changeMe" ,"notToBeChanged":"oldValue" }
},
"2": {
"0": { "test": "changeMe" ,"notToBeChanged":"oldValue" },
"1": { "test": "changeMe" ,"notToBeChanged":"oldValue" },
"2": { "test": "changeMe" ,"notToBeChanged":"oldValue" }
}
},
"2": {
"0": {
"0": { "test": "changeMe" ,"notToBeChanged":"oldValue" },
"1": { "test": "changeMe" ,"notToBeChanged":"oldValue" },
"2": { "test": "changeMe" ,"notToBeChanged":"oldValue" }
},
"1": {
"0": { "test": "changeMe" ,"notToBeChanged":"oldValue" },
"1": { "test": "changeMe" ,"notToBeChanged":"oldValue" },
"2": { "test": "changeMe" ,"notToBeChanged":"oldValue" }
},
"2": {
"0": { "test": "changeMe" ,"notToBeChanged":"oldValue" },
"1": { "test": "changeMe" ,"notToBeChanged":"oldValue" },
"2": { "test": "changeMe" ,"notToBeChanged":"oldValue" }
}
}
};