admin管理员组文章数量:1352878
I have an array that looks like this
const array: any[] = []
array.push({ 'Comments': thisment, 'Name': this.name, 'Description' : this.description })
I pass that array back to a parent ponent. How can I grab the value that is in Comments?
I have an array that looks like this
const array: any[] = []
array.push({ 'Comments': this.ment, 'Name': this.name, 'Description' : this.description })
I pass that array back to a parent ponent. How can I grab the value that is in Comments?
Share Improve this question edited Oct 19, 2023 at 13:21 hlovdal 28.3k11 gold badges100 silver badges175 bronze badges asked Sep 30, 2017 at 15:29 CodeMan03CodeMan03 2354 gold badges25 silver badges49 bronze badges 3- What have you tried, how did it not work? Or do you just not know the basic access operations? – Bergi Commented Sep 30, 2017 at 15:31
- 3 This has nothing to do with TypeScript nor AngularJS. – Andreas Commented Sep 30, 2017 at 15:31
- Suggest changing the title to remove 'typescript' – isimmons Commented Jan 11, 2023 at 5:04
4 Answers
Reset to default 5You can use forEach loop :
const mentArray = [];
array.forEach(function(object) {
var ment = object.Comments;
mentArray.push(ment);
});
//You can store all ments in another array and use it...
console.log("This is ment array...", mentArray);
Or use map but it will work in new browsers possibly ones following ES6:
const mentArray = array.map(function(object) {
return object.Comments;
});
console.log("This is ment array... ", mentArray);
As long as TGW's answer is "correct" and works, I think you should be aware of and use for...of (and for...in) construction, it's superior over Array.forEach (faster, you can use continue and break keywords, better readability) and more often you want to iterate over your array and do things with it, than just get one property (which Array.map is perfect for in this case :) )
You may try for this:
We can also use filter here.
let mentArray:any[];
array.filter(function(object) {
var ment = object.Comments;
mentArray.push(ment);
});
//Store here and use this.
console.log("This is ment array...", mentArray);
You can use Underscore JS for short and better performance like below:
let ids: Array<number> = _.map(this.mylist, (listObj) => {
return listObj.Id;
});
本文标签: How to get a value out of an array in javascriptStack Overflow
版权声明:本文标题:How to get a value out of an array in javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743896336a2557852.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论