【发布时间】:2017-09-10 01:39:40
【问题描述】:
我有 3 个字符串需要转换为数组,从那里我想过滤然后使用 javascript 组合它们,我需要注意我使用的是 Zapier,他们的 javascript 库有点受限,但这就是我到目前为止:
字符串:
var type = 'bundle, simple, simple';
var name = 'Product1, Product2, Product3';
var price = '1.99, 2.99, 3.99';
我需要弄清楚如何使用javascript将上面的3个字符串转换为以下数组:
var itemArray = [
{type:"bundle", info: {name: "Product1", price: "1.99"}},
{type:"simple", info: {name: "Product2", price: "2.99"}},
{type:"simple", info: {name: "Product3", price: "3.99"}}];
从那里我希望过滤掉 bundle 产品类型,只传递 simple 产品数组,我正在这样做:
// Using a for loop
var filtered = [];
for (var i = 0; i < itemArray.length; ++i) {
var item = itemArray[i];
if (item.type == 'simple') filtered.push(item);
}
return {filtered}; //this returns just the 2 simple product type arrays
所以我的问题是,如何获取我开始使用的这 3 个字符串并使用 javascript 将它们转换为我的 itemArray 格式?
【问题讨论】:
-
您确定要
[simple, {product2, 2.99}]还是要[simple, product2, 2.99]和其他数组一样? -
@PhillipMartin 可能也可以,理想情况下,我希望每个产品名称和价格在其对应的产品类型下
-
为什么是三个数组?使用对象!即
[{type:bundle,name:prod1,price:1.99},{type:simple,name:prod2,price:2.99}] -
你可以在这里使用
zip,见stackoverflow.com/questions/4856717/… -
谢谢大家!我更新了我的问题,使其更加具体,并添加了我正在使用的当前代码。
标签: javascript arrays zapier