HTML5没有将两个对象分开

时间:2019-10-12 12:10:56

标签: arrays angular typescript ngfor

无法显示两个对象

this._formsServices.fetching(this._route.snapshot.paramMap.get('id')).subscribe(post => {
          this.field = this._formsServices.form;
          let comma: any;
          this.field.forEach(option => {
            if (option.optionEn) {
              this.options = []; <-----
              comma = option.optionEn.split(',');
              for (const key in comma) {
                if (comma.hasOwnProperty(key)) {
                    this.options.push({ label: comma[key], value: option.fieldType });
                }
              }
            }
          })
        });

输出控制台是正确的,但在HTML5上却没有显示,始终集中在最后一个对象上

(2) [{…}, {…}]
0: {label: "name1", value: "1"}
1: {label: "name1", value: "1"}
length: 2
__proto__: Array(0)
forms.component.ts:91 
(2) [{…}, {…}]
0: {label: "name2", value: "6"}
1: {label: "name2", value: "6"}

1 个答案:

答案 0 :(得分:0)

这是因为您正在每个循环中擦除const sumZeroIndexPairs = arr => { const hashMap = arr.reduce( (result, value, index, list) => { list.forEach((n, i) => { const key = [ index, i ].sort().join('_') const sum = value + n result[key] = sum }) return result }, {} ); return Object.entries(hashMap) .filter( ([key, value]) => value === 0 ) .map(([key]) => key.split('_')); } console.log(sumZeroIndexPairs([1, 2, 3, -1, 5, -5, 7, 9, -7, 2, -2]))中的数据。在第一个循环中,您将这两个值this.options推入了数组,在第二个循环中,您正在创建新的数组0: {label: "name1", value: "1"} 1: {label: "name1", value: "1"}。删除此行,应该可以。