Javascript 使用forEach访问数组内的对象值?
嗨,我的第一个问题。我有一个如下所示的对象数组:Javascript 使用forEach访问数组内的对象值?,javascript,arrays,nested,javascript-objects,Javascript,Arrays,Nested,Javascript Objects,嗨,我的第一个问题。我有一个如下所示的对象数组: const albums = [{title: 'title1', id: 1}, {title: 'title2', id: 2}, {title: 'title3', id: 3}] albums.forEach(function(key, index, arr) { arr[index].title }) var Album = function(arr){ this.arr = arr; th
const albums = [{title: 'title1', id: 1}, {title: 'title2', id: 2},
{title: 'title3', id: 3}]
albums.forEach(function(key, index, arr) {
arr[index].title })
var Album = function(arr){
this.arr = arr;
this.get = function(id){
this.arr.forEach((element) => {
if (element.id == id) {
console.log(element.title)
}
})
}
}
a = new Album([{title: 'title1', id: 2}]);
a.get(2)
从这个数组中,我想按id提取title的动态值。这意味着如果我在这个数组上运行函数,当id被指定为“1”时,我希望收到“title1”。我尝试了很多方法,包括现在看起来像这样的forEach循环:
const albums = [{title: 'title1', id: 1}, {title: 'title2', id: 2},
{title: 'title3', id: 3}]
albums.forEach(function(key, index, arr) {
arr[index].title })
var Album = function(arr){
this.arr = arr;
this.get = function(id){
this.arr.forEach((element) => {
if (element.id == id) {
console.log(element.title)
}
})
}
}
a = new Album([{title: 'title1', id: 2}]);
a.get(2)
我在本网站的其他地方读到for/forEach循环在从对象数组中的对象提取值时非常有用。但我想我并不完全理解forEach循环在这方面是如何工作的,因为我无法从中提取任何值。如果在forEach循环中调用console.log(类似于console.log(arr[index].title)
它会很好地记录数组中每个元素的title属性值。但是当我尝试返回(return arr[index].title
)并调用函数时,它会显示为未定义
总之,我有两个问题:
const albums=[{title:'title1',id:1},{title:'title2',id:2},{title:'title3',id:3}]
var-idToFind=2;
相册.forEach((元素)=>{
if(element.id==idToFind){
console.log(element.title)
}
})
尝试以下代码:
const albums=[{title:'title1',id:1},{title:'title2',id:2},{title:'title3',id:3}]
var-idToFind=2;
相册.forEach((元素)=>{
if(element.id==idToFind){
console.log(element.title)
}
})
试试这个:
function getTitleById(id)
{
var i;
for (i = 0; i < albums.length; i++) {
if(albums[i]['id'] == id)
return albums[i]['title'];
}
}
函数getTitleById(id)
{
var i;
对于(i=0;ifunction getTitleById(id)
{
var i;
for (i = 0; i < albums.length; i++) {
if(albums[i]['id'] == id)
return albums[i]['title'];
}
}
函数getTitleById(id)
{
var i;
对于(i=0;iconsole.log(albums[4].title)
,即可访问它。但如果您不知道位置,则需要遍历每个对象并搜索相关对象。最好的方法是编写一个返回对象的搜索函数const albums=[{title:'title1',id:1},{title:'title2',id:2},
{标题:'标题3',id:3}];
日志(SeracharyByTitle(相册,'title2'));
函数SeracharyByTitle(arrayToSerach,titleToSearchFor){
对于(var i=0;i
如果您知道对象在数组中的位置,只需使用position.econsole.log(albums[4].title)
,即可访问它。但如果您不知道位置,则需要遍历每个对象并搜索相关对象。最好的方法是编写一个返回对象的搜索函数
const albums=[{title:'title1',id:1},{title:'title2',id:2},
{标题:'标题3',id:3}];
日志(SeracharyByTitle(相册,'title2'));
函数SeracharyByTitle(arrayToSerach,titleToSearchFor){
对于(var i=0;iJavaScript,有几种方法可以提取和操作数组中的对象。以下是与您的问题相关的一些最常见的方法:
const albums=[{title:'title1',id:1},{title:'title2',id:2},
{标题:'标题3',id:3}];
//因为每一个都是为了做一些会引起副作用的事情
albums.forEach(({title})=>console.log(title))
//一无所获
//控制台输出:
//标题1
//标题2
//标题3
//映射用于将数组中的每个对象转换为其他对象
const titles=albums.map(({title})=>title);
//=>['title1','title2',title3']
//如果您需要查找特定的,请使用“查找”
const albumWithId3=albums.find(({id})=>id==3);
//=>{title:'title3',id:3}
//如果需要查找子集,请使用过滤器
const-ablumsWithIdsLessThan3=albums.filter(({id})=>id<3)
//=>[{title:'title1',id:1},{title:'title2',id:2}]
JavaScript有几种方法可以提取和操作数组中的对象。以下是与您的问题相关的一些最常见的方法:
const albums=[{title:'title1',id:1},{title:'title2',id:2},
{标题:'标题3',id:3}];
//因为每一个都是为了做一些会引起副作用的事情
albums.forEach(({title})=>console.log(title))
//一无所获
//控制台输出:
//标题1
//标题2
//标题3
//映射用于将数组中的每个对象转换为其他对象
const titles=albums.map(({title})=>title);
//=>['title1','title2',title3']
//如果您需要查找特定的,请使用“查找”
const albumWithId3=albums.find(({id})=>id==3);
//=>{title:'title3',id:3}
//如果需要查找子集,请使用过滤器
const-ablumsWithIdsLessThan3=albums.filter(({id})=>id<3)
//=>[{title:'title1',id:1},{title:'title2',id:2}]
为了一致性,应该使用forEach作用域,如下所示:
const albums = [{title: 'title1', id: 1}, {title: 'title2', id: 2},
{title: 'title3', id: 3}]
albums.forEach(function(key, index, arr) {
arr[index].title })
var Album = function(arr){
this.arr = arr;
this.get = function(id){
this.arr.forEach((element) => {
if (element.id == id) {
console.log(element.title)
}
})
}
}
a = new Album([{title: 'title1', id: 2}]);
a.get(2)
这应该返回“title1”为了保持一致性,您应该使用forEach scoped,如下所示:
const albums = [{title: 'title1', id: 1}, {title: 'title2', id: 2},
{title: 'title3', id: 3}]
albums.forEach(function(key, index, arr) {
arr[index].title })
var Album = function(arr){
this.arr = arr;
this.get = function(id){
this.arr.forEach((element) => {
if (element.id == id) {
console.log(element.title)
}
})
}
}
a = new Album([{title: 'title1', id: 2}]);
a.get(2)
这应该返回“title1”您可以使用它来查找与所需条件匹配的数组元素
然后,如果存在匹配项,则从该元素返回您想要的任何内容
const albums=[{title:'title1',id:1},{title:'title2',id:2},
{标题:'标题3',id:3}]
常量getTitleById=(id)=>{
const o=albums.find(album=>album.id==id);
返回o&&o.title
}
console.log(getTitleById(2))
log(getTitleById(3))
您可以使用它来查找与所需条件匹配的数组元素
然后,如果存在匹配项,则从该元素返回您想要的任何内容
const albums=[{title:'title1',id:1},{title:'title2',id:2},
{标题:'标题3',id:3}]
常量getTitleById=(id)=>{
const o=albums.find(album=>