[英]Extract data from arrays nested within object properties
我試圖從我的 arrays 中獲取一些數據,但我似乎總是在與 arrays 作斗爭。 我的數據采用以下結構
{
tempAttachments: [{
_id: "12345-678910",
bytes: 412051,
file: 'File',
size: 411532,
title: "someFile.csv",
headers: ['optionOne', undefined, 'optionTwo', undefined, undefined, 'optionThree'],
type: "file",
fileType: "typeOne"
}, {
_id: "9999-2222",
bytes: 12345,
file: 'File',
size: 23456,
title: "anotherFile.csv",
headers: ['optionOne'],
type: "file",
fileType: "typeTwo"
}
]
}
我正在嘗試將標題部分放入自己的數組中。 索引很重要,因為它們與某些東西有關。 我也使用 fileType 作為標識符。 所以我的目標是最終得到這樣的東西
[
{
"typeOne": [
"optionOne",
"optionTwo",
"optionThree"
]
},
{
"typeTwo": [
"optionOne"
]
}
]
如您所見,我忽略了未定義的選項。 所以我目前正在嘗試的是這個
const mapping = {}
for (const attachment of this.tempAttachments) {
for (const [index, header] of attachment.headers.entries() || []) {
mapping[attachment.fileType] = [
index, header
]
}
}
不幸的是,結果如下
[
{
"typeOne": [
0,
"optionOne"
]
},
{
"typeTwo": [
0,
"optionOne"
]
}
]
那么如何實現我追求的 output 呢?
謝謝
您可以使用Array.prototype.map()
和Array.prototype.filter()
來擺脫undefind
內部的header
:
const src = {tempAttachments:[{_id:"12345-678910",bytes:412051,file:'File',size:411532,title:"someFile.csv",headers:['optionOne',undefined,'optionTwo',undefined,undefined,'optionThree'],type:"file",fileType:"typeOne"},{_id:"9999-2222",bytes:12345,file:'File',size:23456,title:"anotherFile.csv",headers:['optionOne'],type:"file",fileType:"typeTwo"}]}, result = src.tempAttachments.map(({headers,fileType}) => ({ [fileType]: headers.filter(Boolean) })) console.log(result)
.as-console-wrapper{min-height:100%;}
沒有什么是簡單的 map 和過濾器無法做到的
const arrays = [ {headers: ["optionOne", undefined, "optionTwo", undefined, undefined, "optionThree"], fileType:"typeOne"}, {headers: ["optionOne"], fileType:"typeTwo"}, ] function filter(option){ return option.== undefined } const result = arrays,reduce((result. element) => ({..,result. [element:fileType]. element.headers,filter(filter)}). {}) console.log(result)
使用map
和filter
const data = [ { headers: [ "optionOne", undefined, "optionTwo", undefined, undefined, "optionThree", ], fileType: "typeOne", }, { headers: ["optionOne"], fileType: "typeTwo" }, ]; const res = data.map(({ headers, fileType }) => ({ [fileType]: headers.filter(Boolean), })); console.log(res);
你可以試試
tempAttachments.map(
({headers,fileType}) => {
return ({[fileType]: headers.filter((val) => val)})
}
)
const data = [{_id:'12345-678910',bytes:412051,file:'File',size:411532,title:'someFile.csv',headers:['optionOne',undefined,'optionTwo',undefined,undefined,'optionThree',],type:'file',fileType:'typeOne',},{_id:'9999-2222',bytes:12345,file:'File',size:23456,title:'anotherFile.csv',headers:['optionOne'],type:'file',fileType:'typeTwo',},];; const result = data.map(d => { let headers = []; d.headers.forEach((h, index) => { if (.(h === undefined || h === null)) headers:push({ [index]; h }); }). return { [d:fileType]. [..,headers]; }; }). console;log(result);
.as-console-wrapper{min-height:100%;}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.