Javascript 如何循环使用flat()创建的数组?

Javascript 如何循环使用flat()创建的数组?,javascript,arrays,loops,nested,Javascript,Arrays,Loops,Nested,我使用flat()将3个JSON文件组合到一个数组中,然后在该数组中循环以在控制台中输出其内容。它输出由下面3个JSON文件创建的数组 以下是控制台输出: {books: Array(3)}books: (3) [{…}, {…}, {…}]__proto__: Object {movies: Array(3)}movies: (3) [{…}, {…}, {…}]__proto__: Object {posts: Array(3)}posts: (3) [{…}, {…}, {…}]__pro

我使用flat()将3个JSON文件组合到一个数组中,然后在该数组中循环以在控制台中输出其内容。它输出由下面3个JSON文件创建的数组

以下是控制台输出:

{books: Array(3)}books: (3) [{…}, {…}, {…}]__proto__: Object
{movies: Array(3)}movies: (3) [{…}, {…}, {…}]__proto__: Object
{posts: Array(3)}posts: (3) [{…}, {…}, {…}]__proto__: Object
我在遍历每个内部数组以访问它们的属性/值时遇到问题。谁能帮点忙吗?谢谢大家!

这是我的密码

JAVASCRIPT:

let finalResult;

const urls = ['books', 'movies', 'posts'];

Promise.all(
urls.map(url =>
    fetch('json/' + url + '.json')
        .then(e => e.json())
    )
).then(data => {
    finalResult = data.flat();

    finalResult.forEach(array => {
      console.log(array); // returns the array of arrays I want to loop through to get the property-values
    });
});
books.json

{
  "books": [
    {
      "title": "Eloquent JavaScript, Second Edition",
      "description": "JavaScript lies at the heart of almost every modern web application, from social apps to the newest browser-based games. Though simple for beginners to pick up and play with, JavaScript is a flexible, complex language that you can use to build full-scale applications."
    },
    {
      "title": "Learning JavaScript Design Patterns",
      "description": "With Learning JavaScript Design Patterns, you'll learn how to write beautiful, structured, and maintainable JavaScript by applying classical and modern design patterns to the language. If you want to keep your code efficient, more manageable, and up-to-date with the latest best practices, this book is for you."
    },
    {
      "title": "Speaking JavaScript",
      "description": "Like it or not, JavaScript is everywhere these days-from browser to server to mobile-and now you, too, need to learn the language or dive deeper than you have. This concise book guides you into and through JavaScript, written by a veteran programmer who once found himself in the same position."
    }
  ]
}
movies.json

{
  "movies": [
    {
      "title": "History of the World Part I",
      "description": "A 1981 American sketch comedy film written, produced, and directed by Mel Brooks. Brooks also stars in the film, playing five roles: Moses, Comicus the stand-up philosopher, Tomás de Torquemada, King Louis XVI, and Jacques, le garçon de pisse."
    },
    {
      "title": "Jaws",
      "description": "a 1975 American thriller film directed by Steven Spielberg, based on Peter Benchley's 1974 novel of the same name. In the film, a man-eating great white shark attacks beachgoers at a summer resort town, prompting police chief Martin Brody (Roy Scheider) to hunt it with the help of a marine biologist (Richard Dreyfuss) and a professional shark hunter (Robert Shaw). "
    },
    {
      "title": "The Exorcist",
      "description": "When a 12-year-old girl is possessed by a mysterious entity, her mother seeks the help of two priests to save her."
    }
  ]
}
posts.json

{
  "posts": [
    {
      "title": "Done",
      "description": "I can't take it anymore."
    },
    {
      "title": "Finished",
      "description": "The story of a young man who has finished his sandwich."
    },
    {
      "title": "Concluded",
      "description": "An epic take of a meeting that has conluded."
    }
  ]
}

我不确定这是否正是您想要的,因为您没有指定确切的输出,但我认为您得到了一个如何实现的要点

Object.entries
/
Object.keys
/
Object.values
是您要查找的内容:

constbooks={“books”:[{“title”:“雄辩的JavaScript,第二版”,“description”:“JavaScript是几乎所有现代web应用程序的核心,从社交应用程序到最新的基于浏览器的游戏。虽然对于初学者来说简单易学,但JavaScript是一种灵活、复杂的语言,可以用来构建全面的应用程序。”},{“title”:“学习JavaScript设计模式”,“description”:”通过学习JavaScript设计模式,您将学习如何通过将经典和现代设计模式应用到语言中来编写美观、结构化和可维护的JavaScript。如果您想让代码高效、更易于管理,并与最新的最佳实践保持同步,本书适合您。“},{“title”:“Speaking JavaScript”description:“不管你喜欢与否,JavaScript现在无处不在,从浏览器到服务器再到手机,现在你也需要学习这种语言,或者比你所学的更深入。这本简明的书引导你进入并通过JavaScript,这本书由一位资深程序员编写,他曾经发现自己处于同样的位置。”}]}
康斯特电影公司={“电影”:[{“标题”:“世界历史第一部分”,“描述”:“1981年由梅尔·布鲁克斯撰写、制作和导演的美国喜剧速写电影。布鲁克斯还主演了这部电影,扮演五个角色:摩西、独立哲学家科米库斯、托马斯德·托克马达、国王路易十六和雅克·勒加松·德皮塞。”},{“标题”:“大白鲨”描述:“1975年由史蒂文·斯皮尔伯格执导的美国惊悚片,改编自彼得·本切利1974年同名小说。影片中,一条食人大白鲨袭击了一个避暑胜地的海滩游客,促使警察局长马丁·布罗迪(罗伊·谢德)在海洋生物学家(理查德·德雷福斯)的帮助下追捕它职业猎鲨者(罗伯特·肖)。“},{“头衔”:“驱魔者”,“描述”:“当一个12岁的女孩被一个神秘的实体附身时,她的母亲寻求两名牧师的帮助来救她。”}]}
const posts={“posts”:[{“title”:“Done”,“description”:“我再也受不了了”。},{“title”:“Finished”,“description”:“一个吃了三明治的年轻人的故事”。},{“title”:“Concluded”,“description”:“一次会议的史诗性记录”。}]
const yourThen=(数据)=>{
const finalResult=data.map(e=>Object.entries(e)).flat()
finalResult.forEach(([k,v])=>console.log(k,v))
返回最终结果
}
yourThen([书籍、帖子、电影])

.as控制台包装{max height:100%!important;top:0;}
使用
对象。条目
和解构

const finalResult=[{“books”:[{“title”:“雄辩的JavaScript,第二版”,“description”:”JavaScript是几乎所有现代web应用程序的核心,从社交应用程序到最新的基于浏览器的游戏。虽然对于初学者来说简单易学,但JavaScript是一种灵活、复杂的语言,可以用来构建全面的应用程序。“},{“title”:“学习JavaScript设计模式”,“description”:”通过学习JavaScript设计模式,您将学习如何通过将经典和现代设计模式应用到语言中来编写美观、结构化和可维护的JavaScript。如果您想让代码高效、更易于管理,并与最新的最佳实践保持同步,本书适合您。“},{“title”:“Speaking JavaScript”description:“不管你喜欢与否,JavaScript现在无处不在,从浏览器到服务器再到手机,现在你也需要学习这种语言,或者深入研究。这本简明的书引导你了解JavaScript,这本书是由一位经验丰富的程序员编写的,他曾经发现自己处于同样的地位。”}]},
{“电影”:[{“片名”:“世界历史第一部分”,“描述”:“1981年由梅尔·布鲁克斯创作、制作和导演的美国速写喜剧电影。布鲁克斯还出演了这部电影,扮演五个角色:摩西、独立哲学家科米库斯、托马斯德·托克马达、路易十六国王和雅克·勒加松·德皮塞。”},{“片名”:“大白鲨”,“描述”:”1975年由史蒂文·斯皮尔伯格执导的美国惊悚片,改编自彼得·本切利1974年同名小说。影片中,一条食人大白鲨袭击了一个避暑胜地的海滩游客,促使警察局长马丁·布罗迪(罗伊·谢德)在海洋生物学家(理查德·德雷福斯)的帮助下追捕它职业猎鲨者(罗伯特·肖)。“},{”头衔“:“驱魔者”,“描述“:“当一个12岁的女孩被一个神秘的实体附身时,她的母亲寻求两名牧师的帮助来救她。”}]},
{“posts”:[{“title”:“Done”,“description”:“我再也受不了了”。},{“title”:“Finished”,“description”:“一个吃完三明治的年轻人的故事”。},{“title”:“conclused”,“description”:“一次令人困惑的会议的史诗性记录”。}]]
finalResult.forEach(obj=>{
//解构第一个条目键、值对。
常量[[type,array]]=Object.entries(obj);
console.log('----------->Type:',Type);
forEach(item=>console.log(item));

})
您期望的输出是什么样子的?数据是什么样子的?数据:{books:[…],posts:[…],movies:[…]}?与其说是输出,还不如说是输出。