以下是发生的事情:
我在Shopify商店中使用了一个非常酷的插件,允许您按照一系列步骤自定义您的产品。
问题是所有这些信息都没有为Zapier排序。基本上它显示如下:
它们作为“行项目属性值”和“行项目属性名称”出现,粘贴后它们将作为数组生成。
我需要做的是将这些名称与其值匹配。如果可能的话,可以在Zapier的GUI中进行选择。
所以不要使用这些字段和值
"Line Properties Names" -> "Project Title","Project Description","Ebook Type"....
"Line Items Properties Values" -> "Sherlock Holmes","A story in London..", "Standard Book"...
拥有这些字段和值:
"Project Title" -> "Sherlock Holmes"
"Project Description" -> "A story in London.."
"Ebook Type" -> "Standard Ebook"
有可能吗?
感谢您的时间
更新
出于澄清目的
因此,按此顺序有3种不同的产品。由[]分隔。产品中的值可能会有所不同,例如,如果客户决定不填写“项目详细信息”字段,则不会显示项目详细信息键和值。主要导致产品具有不同数量的键和值。
以下是一个示例:(正如您所看到的,第一个产品的第二个产品具有不同的值集)
输入数据
Input data Values: ["1","ebook1524837342394","Ebook~~//www.shappify-cdn.com/images/282516/127828455/001_Ebook Technical 325x325 (1).png~~ebook","Technical Ebook","Technical 15K - $450.00","All Inclusive Package - $149.00","Cookbook Instant Pot"],["ebook1524837342394"],["Detective Story based in London......","Sherlock Holmes","No Addons","No Package","10000 Words - $270.00","Fiction Book","Ebook~~//www.shappify-cdn.com/images/282516/127828453/001_Ebook Standard 325x325.png~~ebook","ebook1524837304725","1","https://cdn.shopify.com/s/files/1/0012/8814/2906/uploads/7ddee14d6e5d6c5e4396981d1702c375.jpg"]
Input data Keys: ["_master_builder","_builder_id","_builder_info","Ebook Type","Word Count","Upgrade","Project Title"],["_builder_id"],["Project Details","Project Title","Addons","Upgrade","Word Count","Ebook Type","_builder_info","_builder_id","_master_builder","Upload your file here"]
我想做什么
我希望将密钥与其值匹配,并能够在Zapier GUI上选择它们。
使用建议代码的当前输出
预期输出
[{"_master_builder":"1","_builder_id":"ebook1524837342394","_builder_info":"Ebook~~//www.shappify-cdn.com/images/282516/127828455/001_Ebook Technical 325x325 (1).png~~ebook"...}]
感谢你的帮助人员
答案 0 :(得分:1)
啊!所以这比起初看起来有点棘手,但它并不坏。有两个陷阱:
因此,一旦我们正确解析它,它就不会太糟糕。
// just used for testing outside zapier
// these are comma separated strings
const inputData = {
keys: '_master_builder,_builder_id,_builder_info,Ebook Type,Word Count,Upgrade,Project Title,_builder_id,Project Details,Project Title,Addons,Upgrade,Word Count,Ebook Type,_builder_info,_builder_id,_master_builder,Upload your file here',
values: '1,ebook1524837342394,Ebook~~//www.shappify-cdn.com/images/282516/127828455/001_Ebook Technical 325x325 (1).png~~ebook,Technical Ebook,Technical 15K - $450.00,All Inclusive Package - $149.00,Cookbook Instant Pot,ebook1524837342394,Detective Story based in London......,Sherlock Holmes,No Addons,No Package,10000 Words - $270.00,Fiction Book,Ebook~~//www.shappify-cdn.com/images/282516/127828453/001_Ebook Standard 325x325.png~~ebook,ebook1524837304725,1,https://cdn.shopify.com/s/files/1/0012/8814/2906/uploads/7ddee14d6e5d6c5e4396981d1702c375.jpg'
}
// arrays must be the same length
const zipArrays = (a, b) => {
let res = {}
a.forEach((val, i) => {
res[val] = b[i]
})
return res
}
// have to convert strings to actual arrays
// this will blow up if any of the data has commas in it
const keys = inputData.keys.split(',')
const vals = inputData.values.split(',')
// now we have real arrays
const result = {}
// copy keys onto the result, overwriting old ones
Object.assign(result, zipArrays(keys, vals))
console.log(result)
/*
{ _master_builder: '1',
_builder_id: 'ebook1524837304725',
_builder_info: 'Ebook~~//www.shappify-cdn.com/images/282516/127828453/001_Ebook Standard 325x325.png~~ebook',
'Ebook Type': 'Fiction Book',
'Word Count': '10000 Words - $270.00',
Upgrade: 'No Package',
'Project Title': 'Sherlock Holmes',
'Project Details': 'Detective Story based in London......',
Addons: 'No Addons',
'Upload your file here': 'https://cdn.shopify.com/s/files/1/0012/8814/2906/uploads/7ddee14d6e5d6c5e4396981d1702c375.jpg' }
*/
// return result
原样,有很多重复的按键,因此输出小于输入。如果要以不同方式对输出进行分组,还可以调整此代码以更好地匹配输入。
希望有所帮助!
答案 1 :(得分:0)
这应该可以使用for
循环。试试这个。
在example中,将第一个框定义为值,将第二个框定义为键。在运行代码步骤之前删除var inputData...
行。
var inputData = {"keys": ["First", "Second", "Third"],
"values": ["One", "Two", "Three"]
};
//Remove the lines above before pasting in the Code step.
//You will need to configure it in the Zap.
var product = {};
for (var i = 0; i < inputData.keys.length; i++) {
var commonkey = inputData.keys[i];
product[commonkey] = inputData.values[i];
}
console.log(JSON.stringify([product]));
// already available in the zapier scope
output = [product]