简体   繁体   English

访问嵌套数组/对象 - 嵌套 Object 解构

[英]Access a nested Array/Object - Nested Object Destructuring

i'm trying for over a week now, but it seems that i don't understand all the examples i can find online.. Somehow things don't work out for me..我现在正在尝试一个多星期,但似乎我不明白我可以在网上找到的所有示例..不知何故,事情对我来说并不奏效..

I get the JSON as a response from an API and i want to access the Informations inside the ArticleList Array.我得到 JSON 作为 API 的响应,我想访问 ArticleList 数组中的信息。 But when i try to console.log something it says "undefined" .. For testing purposes, i added a function that returns the JSON so i can test it without calling the API.但是当我尝试 console.log 一些它说“未定义”的东西时。为了测试目的,我添加了一个 function,它返回 JSON,所以我可以在不调用 ZDB974238714CA8DE6434A7CE1DZ08A 的情况下对其进行测试。

This is the latest version of my code:这是我的代码的最新版本:

 function makeArrayOfArticleList() { return { data: { FinalHelperCombinationList: [], FinalCombinationList: [ { ArticleList: [ { Id: 54, Description: "VT-U 20mm/4x10-20/140(30)", Number: "719241", Datasheet: "04.06-01_vt-u.pdf", }, { Id: 54, Description: "VT-U 20mm/4x10-20/140(30)", Number: "719241", Datasheet: "04.06-01_vt-u.pdf", }, ], TotalSize: 40, HighSize: 40, Category1: false, Category2: false, Category3: false, Category4: false, Category6: false, Category7: false, CombinationScore: 0, }, { ArticleList: [ { Id: 113, Description: "VT-U 30mm/4x10-20/140(24)", Number: "719230", Datasheet: "04.06-01_vt-u.pdf", }, { Id: 114, Description: "VT-U 10mm/4x10-20/140(40)", Number: "719240", Datasheet: "04.06-01_vt-u.pdf", }, ], TotalSize: 40, HighSize: 40, Category1: false, Category2: false, Category3: false, Category4: false, Category6: false, Category7: false, CombinationScore: 0, }, { ArticleList: [ { Id: 51, Description: "VT-B 15mm/4x10-15/140, 4Stege(40)", Number: "719041", Datasheet: "04.05-01_vt-b.pdf", }, { Id: 51, Description: "VT-B 15mm/4x10-15/140, 4Stege(40)", Number: "719041", Datasheet: "04.05-01_vt-b.pdf", }, { Id: 114, Description: "VT-U 10mm/4x10-20/140(40)", Number: "719240", Datasheet: "04.06-01_vt-u.pdf", }, ], TotalSize: 40, HighSize: 40, Category1: false, Category2: false, Category3: false, Category4: false, Category6: false, Category7: false, CombinationScore: 0, }, ], }, }; } const { FinalCombinationList: { ArticleList } } = makeArrayOfArticleList; console.log(ArticleList);

I appreciate your help, thanks.感谢您的帮助,谢谢。

Greetings Erik问候埃里克

Try to destructure like this, since ArticleList is array type:尝试像这样解构,因为 ArticleList 是数组类型:

const {
  data: {
    FinalCombinationList: [ArticleList],
  },
} = makeArrayOfArticleList();

See @VLAZ comment for further details.有关详细信息,请参阅@VLAZ 评论。

I don't this destrucing would work here, this returns just one array of ArticleList.我不认为这种破坏会在这里起作用,它只返回一个 ArticleList 数组。 In the examples below map or foreach is used to get all the data.在下面的示例中 map 或 foreach 用于获取所有数据。

This creates an array with the article lists combined based of the function;这将创建一个包含基于 function 的文章列表组合的数组;

With this you can add code to skip duplicates:有了这个,您可以添加代码以跳过重复项:

let articleList = [];
makeArrayOfArticleList().data.FinalCombinationList.forEach(e=> articleList = articleList.concat(e.ArticleList));
console.log(articleList);

Or just (which just outputs all articlelists combined:或者只是(仅输出所有文章列表的组合:

makeArrayOfArticleList().data.FinalCombinationList.flatMap(e=> e.ArticleList);

Or as requested, split in array for each ArticleList或根据要求,为每个 ArticleList 拆分为数组

makeArrayOfArticleList().data.FinalCombinationList.map(e=> e.ArticleList);

From the above comment...从上面的评论...

"The OP has to destructure/assign FinalCombinationList correctly in first place (and actually call the makeArrayOfArticleList function)... const { data: { FinalCombinationList } } = makeArrayOfArticleList(); ... 2nd, there are more than just a single ArticleList within FinalCombinationList . Thus said, what does the OP want ArticleList to be like?" “OP 必须首先正确地解构/分配FinalCombinationList (并实际调用makeArrayOfArticleList函数)... const { data: { FinalCombinationList } } = makeArrayOfArticleList(); ... 第二,不仅仅是一个ArticleListFinalCombinationList中。因此,OP 希望ArticleList是什么样的?

If all the ArticleList s of any of a FinalCombinationList 's item are supposed to be aggregated/concatenated into a single array then flatMap is the right method to go with, whereas map does return an array of ArticleList s...如果ArticleList的任何项目的所有FinalCombinationList都应该聚合/连接到单个数组中,那么flatMap是 go 的正确方法,而map确实返回ArticleList的数组...

 const { data: { FinalCombinationList } } = makeArrayOfArticleList(); const aggregatedArticleList = FinalCombinationList.flatMap(({ ArticleList }) => ArticleList); const listOfArticleLists = FinalCombinationList.map(({ ArticleList }) => ArticleList); console.log({ aggregatedArticleList, listOfArticleLists });
 .as-console-wrapper { min-height: 100%;important: top; 0; }
 <script> function makeArrayOfArticleList() { return { data: { FinalHelperCombinationList: [], FinalCombinationList: [{ ArticleList: [{ Id: 54, Description: "VT-U 20mm/4x10-20/140(30)", Number: "719241", Datasheet: "04.06-01_vt-u.pdf", }, { Id: 54, Description: "VT-U 20mm/4x10-20/140(30)", Number: "719241", Datasheet: "04.06-01_vt-u.pdf", }], TotalSize: 40, HighSize: 40, Category1: false, Category2: false, Category3: false, Category4: false, Category6: false, Category7: false, CombinationScore: 0, }, { ArticleList: [{ Id: 113, Description: "VT-U 30mm/4x10-20/140(24)", Number: "719230", Datasheet: "04.06-01_vt-u.pdf", }, { Id: 114, Description: "VT-U 10mm/4x10-20/140(40)", Number: "719240", Datasheet: "04.06-01_vt-u.pdf", }], TotalSize: 40, HighSize: 40, Category1: false, Category2: false, Category3: false, Category4: false, Category6: false, Category7: false, CombinationScore: 0, }, { ArticleList: [{ Id: 51, Description: "VT-B 15mm/4x10-15/140, 4Stege(40)", Number: "719041", Datasheet: "04.05-01_vt-b.pdf", }, { Id: 51, Description: "VT-B 15mm/4x10-15/140, 4Stege(40)", Number: "719041", Datasheet: "04.05-01_vt-b.pdf", }, { Id: 114, Description: "VT-U 10mm/4x10-20/140(40)", Number: "719240", Datasheet: "04.06-01_vt-u.pdf", }], TotalSize: 40, HighSize: 40, Category1: false, Category2: false, Category3: false, Category4: false, Category6: false, Category7: false, CombinationScore: 0, }], }, }; } </script>

enter code here

 // I have put your data into a variable; then started destructuring; //at the end of this code you will find the data from the array ArticleList in tebular form. const jsonResult = { data: { FinalHelperCombinationList: [], FinalCombinationList: [ { ArticleList: [ { Id: 54, Description: "VT-U 20mm/4x10-20/140(30)", Number: "719241", Datasheet: "04.06-01_vt-u.pdf", }, { Id: 54, Description: "VT-U 20mm/4x10-20/140(30)", Number: "719241", Datasheet: "04.06-01_vt-u.pdf", }, ], TotalSize: 40, HighSize: 40, Category1: false, Category2: false, Category3: false, Category4: false, Category6: false, Category7: false, CombinationScore: 0, }, { ArticleList: [ { Id: 113, Description: "VT-U 30mm/4x10-20/140(24)", Number: "719230", Datasheet: "04.06-01_vt-u.pdf", }, { Id: 114, Description: "VT-U 10mm/4x10-20/140(40)", Number: "719240", Datasheet: "04.06-01_vt-u.pdf", }, ], TotalSize: 40, HighSize: 40, Category1: false, Category2: false, Category3: false, Category4: false, Category6: false, Category7: false, CombinationScore: 0, }, { ArticleList: [ { Id: 51, Description: "VT-B 15mm/4x10-15/140, 4Stege(40)", Number: "719041", Datasheet: "04.05-01_vt-b.pdf", }, { Id: 51, Description: "VT-B 15mm/4x10-15/140, 4Stege(40)", Number: "719041", Datasheet: "04.05-01_vt-b.pdf", }, { Id: 114, Description: "VT-U 10mm/4x10-20/140(40)", Number: "719240", Datasheet: "04.06-01_vt-u.pdf", }, ], TotalSize: 40, HighSize: 40, Category1: false, Category2: false, Category3: false, Category4: false, Category6: false, Category7: false, CombinationScore: 0, }, ], }, }; // Starting of Destructuring const { data: { FinalCombinationList: articles }, } = jsonResult; //variable "articles" is an array containing objects as its elements. // so applying froEach loop to the array "articles" articles.forEach((article) => { // after looping through "articles" we get nested objects "article" // Destructuring "article" to get ArticleList. ArticleList will be arrays. const { ArticleList } = article; //applying froEach loop to the array "ArticleList" ArticleList.forEach((articleInfo) => { // to show data in tebular format console.table(articleInfo); //"articleInfo" are objects. //Destructuring object properties const { Id, Description, Number, Datasheet } = articleInfo; console.log(Id, Description, Number, Datasheet); }); }); // run this code with an html file and see the result in console as console.table() will only work in a browser // or if you're using vs code, you can run this code directly

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM