提前为post道歉。 @xavdid做得很好,帮助了我。由于我在这个领域缺乏专业知识和知识,我未能准确地表达我的需求。我相信我现在有足够的信息来表达我想要达到的目标。所以我会尽力在这里表达。
这是我的输入信息,键和值。键的每个位置对应于值的位置。
我相信为了解决这个问题,我需要知道一本书何时开始以及什么时候没有开始。我错了。
我需要的是将预定义的键与其值匹配并将它们组合在一起。
通过预定义的密钥,我的意思是只返回这7个密钥:“项目详细信息,项目标题,插件,升级,字数,电子书类型,在此处上传文件”及其值(忽略其他键)
3本书的例子:
输入数据键: Project Details,Project Title,Addons,Upgrade,Word
Count,Ebook Type,_builder_info,_builder_id,_master_builder,Upload your
file here,_builder_id,_master_builder,_builder_id,_builder_info,Ebook
Type,Word Count,Upgrade,Addons,Project Title,Project Details,Project
Details,Project Title,Addons,Upgrade,Word Count,Ebook
Type,_builder_info,_builder_id,_master_builder,Upload your file
here,_builder_id
输入数据值: Book Description 3,Book Title 3,Book Cover Design - $59.00,No Package,Standard 10K - $270.00,Standard Ebook,Start~~//www.shappify-cdn.com/images/282516/127828454/001_Ebook Standard 325x325.png~~start,start1526659928051,1,https://cdn.shopify.com/s/files/1/0012/8814/2906/uploads/778dfc3dbdf278441776e9f5dd763910.png,start1526659928051,1,start1526659872230,Start~~//www.shappify-cdn.com/images/282516/127828455/001_Ebook Technical 325x325 (1).png~~start,Technical Ebook,Technical 15K - $450.00,No Package,No Addons,Book Title 2,Book Description 2,Book 1 Description,Book 1 Title,No Addons,Essential Package - $79.00,Standard 20K - $540.00,Standard Ebook,Start~~//www.shappify-cdn.com/images/282516/127828458/001_Ebook Standard 20k 325x325.png~~start,start1526659838425,1,https://cdn.shopify.com/s/files/1/0012/8814/2906/uploads/c09635c2e003fd8779a19651e36f4315.png,start1526659838425
所需的输出:
[{'Ebook Type': 'Standard Ebook'},{'Ebook Type':'Technical Ebook'},{'Ebook Type':'Standard Ebook'}],
[{'Word Count': 'Standard 10K - $270.00'}, {'Word Count': 'Technical 15K - $450.00'},{'Word Count': 'Standard 20K - $540.00'}]
[{Upgrade: 'No Package'},{Upgrade: 'No Package'},{Upgrade: 'Essential Package - $79.00'}]
[{Project Title: 'Book Title 3'}, {Project Title: 'Book Title 2'}, {Project Title: 'Book Title 1'}]
[{'Project Details': 'Book Description 3'},{'Project Details': 'Book Description 2'},{'Project Details':'Book 1 Description'}],
[{'Addons: 'Book Cover Design - $59.00',{Addons:'No Addons'},{Addons:'No Addons'}],
[{'Upload your file here':'https://cdn.shopify.com/s/files/1/0012/8814/2906/uploads/778dfc3dbdf278441776e9f5dd763910.png'},{'Upload your file here':https://cdn.shopify.com/s/files/1/0012/8814/2906/uploads/c09635c2e003fd8779a19651e36f4315.png'}]
非常感谢
答案 0 :(得分:0)
确定!所以我想我有这个我们想要它的方式。最大的问题是我们对数据结构的假设,即:
鉴于此,我们需要将输入分开,分成我们想要的7个键,然后从这些数组中构建书籍。这是以下内容:
const keys = inputData.keys.split(',')
const values = inputData.values.split(',')
const keysWeWant = "Project Details,Project Title,Addons,Upgrade,Word Count,Ebook Type,Upload your file here".split(',')
let orderedValues = {}
// fill arrays so we can blindly push
keysWeWant.forEach(k => orderedValues[k] = [])
keys.forEach((key, index) => {
if (keysWeWant.includes(key)) {
orderedValues[key].push(values[index])
}
})
console.log(orderedValues)
// have to cut up books now
// don't know how many we have, hopefully our values have the right number of items
// .map(Object) is because of this: https://stackoverflow.com/questions/35578478/array-prototype-fill-with-object-passes-reference-and-not-new-instance
let result = Array(orderedValues['Project Title'].length)
.fill()
.map(Object)
for (const keyWeWant in orderedValues) {
orderedValues[keyWeWant].forEach((val, index) => {
result[index][keyWeWant] = val
})
}
// put book 1 first, this can be removed
result.reverse()
return result
你可以在行动here中看到它。值得注意的是,在示例数据中,只有两个“在此处上传您的文件”,因此最后一本书(第1册)缺少该密钥。
另外,当您在zap编辑器中测试时,它看起来只返回1个项目。这只是仅适用于测试。当它正在运行时它将正常工作(代码步骤之后的所有内容将对返回的数组中的每个项目运行一次)。