[英]Angular How to Fetch or select data from array
I have two data collection 1 which contains the header data or the column to be displayed.我有两个数据集合 1,其中包含 header 数据或要显示的列。
Another is the actual data source.另一个是实际的数据源。
Now what I have to do from that JSON data I have to select only that data which are in that header list.现在我必须从 JSON 数据到 select 只需要 header 列表中的数据做什么。
Below is the sample of the data以下是数据样本
Header Data Array: ["Execution_Plan_Note", "Budget", "Reporting_Period", "Budget_Note", "Spend_Plan_Note", "Spend_Plan"]
Header 数据数组:
["Execution_Plan_Note", "Budget", "Reporting_Period", "Budget_Note", "Spend_Plan_Note", "Spend_Plan"]
And Actual Data Source和实际数据源
So from the below collection, I want only data that are in the header data array in the new object how I can achieve this if any 1 has an idea let me know i have tried almost everything.因此,从下面的集合中,我只想要新 object 中的 header 数据数组中的数据,如果有任何一个想法让我知道我已经尝试了几乎所有东西,我该如何实现这一点。
You can try with map
and reduce
like below.您可以尝试使用
map
并reduce
如下所示。
const keysArray = ['a', 'b']; const list = [{a: 'a', b: 'b', c: 'c'}, {a: 'a1', b: 'b1', c: 'c1'}, {a: 'a2', b: 'b2', c: 'c2'}]; const result = list.map(item => keysArray.reduce((acc, key) => { acc[key] = item[key]; return acc; }, {})); console.log(result);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.