【发布时间】:2018-11-19 15:16:14
【问题描述】:
我在 angular2 .ts 文件中定义了一个数组 -
this.dropdownValue=[];
然后我从不同的函数中将值推送到它们中 -
this.dropdownValue.push({ item_text: this.organizationInfo.records[i]._fields[0].end.properties.name });
this.dropdownValue.push({ item_text: this.departmentInfo.records[i]._fields[0].end.properties.name });
控制台语句 -
console.log("inside Proceed, this.dropdownValue =", typeof(this.dropdownValue));
console.log("inside Proceed, this.dropdownValue =", this.dropdownValue);
console.log("inside Proceed, this.dropdownValue =", this.dropdownValue[0]);
console.log("inside Proceed, this.dropdownValue =", this.dropdownValue.length);
有几个值的输出是 -
typeOf(this.dropdownValue ) = object
this.dropdownValue =
[
0:{item_text: "IT"}
1:{item_text: "post"}
2:{item_text: "intimationDate"}
3:{item_text: "lossDescription"}
]
this.dropdownValue[0] = undefined
this.dropdownValue.length = 0
我想访问 - 的值
item_text
.即将它存储在另一个变量/数组中。
我无法这样做。我也无法访问这些值,找不到它的长度,也找不到特定长度的值 请帮忙
【问题讨论】:
-
什么是
this.dropdownValue[0] = undefined和this.dropdownValue.length = 0?它是你的输出还是你分配这些值? -
如果文本是唯一的,
this.dropdownValue.find((elem) => elem.item_text === "IT")应该可以工作,developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… -
嗨 Ashish,谢谢。 this.dropdownValue[0] = undefined 和 this.dropdownValue.length = 0 是输出。而且我不想对其进行硬编码。就像 - 你做了 -> === “IT”。我想将整个 item_text 值放入一个变量中
-
如果这些是输出,那么您似乎在填充数组之前正在访问它?可能在一些订阅之外,而在订阅下的数组。它是通过一些异步请求填充的吗?
-
你想存储在一个变量中?那么你会做多少个变量呢?另一个变量是包含
item_text的数组,在这种情况下会更好