【问题标题】:How to find the value of the array object after push in Angular2如何在Angular2中推入后找到数组对象的值
【发布时间】: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] = undefinedthis.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 的数组,在这种情况下会更好

标签: arrays angular object


【解决方案1】:
let otherVariable = this.dropdownValue.find(x => x.item_text === 'IT');

我编辑我的答案是因为我现在不明白你在寻找什么。

let otherArray = []; 
for (let obj of this.dropdownValue) {
 otherArray.push(obj.item_text);
}

我知道这不是最好的方法,但是当我必须做类似的事情时,我总是这样做

【讨论】:

  • 虽然此代码可能会回答问题,但提供有关它如何和/或为什么解决问题的额外上下文将提高​​答案的长期价值。
  • 嗨,马蒂亚斯,感谢您的回复。我纠正了我的问题。我想从数组中累积所有 item_text。
猜你喜欢
  • 2017-01-17
  • 2017-06-10
  • 1970-01-01
  • 2017-04-16
  • 2021-11-06
  • 2017-09-01
  • 1970-01-01
  • 2019-07-15
  • 2023-03-19
相关资源
最近更新 更多