Javascript: 如何合并和过滤数组
Javascript: How to Combine & Filter Arrays
我有 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
所以我的问题是,如何使用 javascript 将我开始使用的那 3 个字符串转换为我的 itemArray
格式?
首先,通过 map
将它们全部组合成一个对象数组,然后 filter
,然后再次 map
组合成您需要的表示形式。类似于:
item_type
.map((type, index) => ({
type,
index,
name: item_name[index],
price: item_price[index]
}))
.filter(el => el.type === 'simple')
.map(el => [el.type, {name: el.name, price: el.price}])
您可以使用map
和filter
的组合,先组合您拥有的三个数组,然后过滤掉匹配item_type='bundle'
.
的数组
var item_type = ['bundle', 'simple', 'simple'],
item_name = ['product1', 'product2', 'product3'],
item_price = [1.99, 2.99, 3.99],
res = item_type.map(function(v,i) {
//combine arrays
return [v, { [item_name[i]]: item_price[i] }];
}).filter(function(o) {
// only allow items where 'item_type' is not "bundle"
return o[0] != "bundle";
});
console.log(JSON.stringify(res, 2, null));
您可以过滤列、转置数组并构建所需的内部数组。
var item_type = ['bundle', 'simple', 'simple'],
item_name = ['product1', 'product2', 'product3'],
item_price = [1.99, 2.99, 3.99],
result = [item_type, item_name, item_price]
.map((a, _, aa) => a.filter((b, i) => aa[0][i] !== 'bundle'))
.reduce((r, a, i) => (a.forEach((b, j) => (r[j] = r[j] || [], r[j][i] = b)), r), [])
.map(a => ({ type: a[0], info: { name: a[1], price: a[2] } }));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
是的...JS 缺少 Array.prototype.zip()
功能。让我们发明它并相应地解决它。
Array.prototype.zip = function(...a){
return this.map((e,i) => [e].concat(a.map(sa => sa[i])));
};
var itemType = ["bundle", "simple", "simple"],
itemName = ["product1", "product2", "product3"],
itemPrice = [1.99,2.99,3.99],
result = itemType.zip(itemName,itemPrice)
.map(sa => [sa[0],{[sa[1]]:sa[2]}])
.filter(t => t[0] === "simple");
console.log(result);
PS:我已经交换了最后一个 .map()
和 .filter()
函数的位置以满足您的要求,但是在 SO 中不鼓励修改问题以产生先前答案中的变化.
我有 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
所以我的问题是,如何使用 javascript 将我开始使用的那 3 个字符串转换为我的 itemArray
格式?
首先,通过 map
将它们全部组合成一个对象数组,然后 filter
,然后再次 map
组合成您需要的表示形式。类似于:
item_type
.map((type, index) => ({
type,
index,
name: item_name[index],
price: item_price[index]
}))
.filter(el => el.type === 'simple')
.map(el => [el.type, {name: el.name, price: el.price}])
您可以使用map
和filter
的组合,先组合您拥有的三个数组,然后过滤掉匹配item_type='bundle'
.
var item_type = ['bundle', 'simple', 'simple'],
item_name = ['product1', 'product2', 'product3'],
item_price = [1.99, 2.99, 3.99],
res = item_type.map(function(v,i) {
//combine arrays
return [v, { [item_name[i]]: item_price[i] }];
}).filter(function(o) {
// only allow items where 'item_type' is not "bundle"
return o[0] != "bundle";
});
console.log(JSON.stringify(res, 2, null));
您可以过滤列、转置数组并构建所需的内部数组。
var item_type = ['bundle', 'simple', 'simple'],
item_name = ['product1', 'product2', 'product3'],
item_price = [1.99, 2.99, 3.99],
result = [item_type, item_name, item_price]
.map((a, _, aa) => a.filter((b, i) => aa[0][i] !== 'bundle'))
.reduce((r, a, i) => (a.forEach((b, j) => (r[j] = r[j] || [], r[j][i] = b)), r), [])
.map(a => ({ type: a[0], info: { name: a[1], price: a[2] } }));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
是的...JS 缺少 Array.prototype.zip()
功能。让我们发明它并相应地解决它。
Array.prototype.zip = function(...a){
return this.map((e,i) => [e].concat(a.map(sa => sa[i])));
};
var itemType = ["bundle", "simple", "simple"],
itemName = ["product1", "product2", "product3"],
itemPrice = [1.99,2.99,3.99],
result = itemType.zip(itemName,itemPrice)
.map(sa => [sa[0],{[sa[1]]:sa[2]}])
.filter(t => t[0] === "simple");
console.log(result);
PS:我已经交换了最后一个 .map()
和 .filter()
函数的位置以满足您的要求,但是在 SO 中不鼓励修改问题以产生先前答案中的变化.