Javascript: 将字符串组合成数组
Javascript: Combining Strings into Array
我有 3 个字符串需要转换成一个数组,我想从那里过滤掉 type: "bundle"
。
我需要注意,我使用的是 Zapier 的 Javascript 代码,他们的 javascript 库在我可以使用的功能方面有点受限,但这就是我所拥有的到目前为止,如果我硬编码itemArray
,哪个有效。我只是无法从 3 个给定的字符串创建我的 itemArray
:
字符串:
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
产品类型,并且只过滤掉 return 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
所以我的问题是,如何使用 javascript 将我开始使用的那 3 个字符串转换为我的 itemArray
格式?
首先把字符串做成你想要的三个字符串的数组。然后在 for 循环中,您可以将它们全部推送到您想要的任何(相同)格式,因为所有 3 个列表各有 3 个元素。然后你可以使用过滤器功能轻松过滤出如下所示的捆绑元素。
以下代码段将打印出项目数组和您请求的过滤值
var types = 'bundle, simple, simple'.split(", ");
var names = 'Product1, Product2, Product3'.split(", ");
var prices = '1.99, 2.99, 3.99'.split(", ");
var itemArray = [];
for(var i = 0; i < 3; i++){
itemArray.push({"type": types[i], "info":{"name": names[i], "price": prices[i]}});
}
console.log(itemArray);
var filtered = [];
for (var i = 0; i < itemArray.length; ++i) {
var item = itemArray[i];
if (item["type"] === 'simple') filtered.push(item);
}
console.log({filtered});
var type = 'bundle, simple, simple'.split(', '), // split the
nameArr = 'Product1, Product2, Product3'.split(', '), // strings to
priceArr = '1.99, 2.99, 3.99'.split(', '), // get the arrays
res = type.map((v,i) => Object.assign({}, {type: v, info: {name: nameArr[i], price: priceArr[i]}})), //map the objects with specified keys and values from given arrays
result = res.filter(v => v.type != 'bundle'); //remove the `bundle` type elements
console.log(result);
我有 3 个字符串需要转换成一个数组,我想从那里过滤掉 type: "bundle"
。
我需要注意,我使用的是 Zapier 的 Javascript 代码,他们的 javascript 库在我可以使用的功能方面有点受限,但这就是我所拥有的到目前为止,如果我硬编码itemArray
,哪个有效。我只是无法从 3 个给定的字符串创建我的 itemArray
:
字符串:
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
产品类型,并且只过滤掉 return 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
所以我的问题是,如何使用 javascript 将我开始使用的那 3 个字符串转换为我的 itemArray
格式?
首先把字符串做成你想要的三个字符串的数组。然后在 for 循环中,您可以将它们全部推送到您想要的任何(相同)格式,因为所有 3 个列表各有 3 个元素。然后你可以使用过滤器功能轻松过滤出如下所示的捆绑元素。 以下代码段将打印出项目数组和您请求的过滤值
var types = 'bundle, simple, simple'.split(", ");
var names = 'Product1, Product2, Product3'.split(", ");
var prices = '1.99, 2.99, 3.99'.split(", ");
var itemArray = [];
for(var i = 0; i < 3; i++){
itemArray.push({"type": types[i], "info":{"name": names[i], "price": prices[i]}});
}
console.log(itemArray);
var filtered = [];
for (var i = 0; i < itemArray.length; ++i) {
var item = itemArray[i];
if (item["type"] === 'simple') filtered.push(item);
}
console.log({filtered});
var type = 'bundle, simple, simple'.split(', '), // split the
nameArr = 'Product1, Product2, Product3'.split(', '), // strings to
priceArr = '1.99, 2.99, 3.99'.split(', '), // get the arrays
res = type.map((v,i) => Object.assign({}, {type: v, info: {name: nameArr[i], price: priceArr[i]}})), //map the objects with specified keys and values from given arrays
result = res.filter(v => v.type != 'bundle'); //remove the `bundle` type elements
console.log(result);