-1

I have 3 strings that I need to convert into arrays, from there I want to filter and then combine them using javascript, I need to note that I'm using Zapier and their javascript library is a bit limited but this is what I have so far:

Strings:

var type  = 'bundle, simple, simple';
var name  = 'Product1, Product2, Product3';
var price = '1.99, 2.99, 3.99';

I need to figure out how to convert the above 3 strings above into the following array using javascript:

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"}}];

From there I'm looking to filter out the bundle product type and only pass along the simple product arrays, I'm doing that with the following:

// 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

So my question is, how do I take those 3 strings that I began with and convert those into my itemArray format using javascript?

AJK
  • 391
  • 9
  • 27
  • Are you sure you want `[simple, {product2, 2.99}]` or do you want `[simple, product2, 2.99]` and same for the other array? – Phillip Martin Apr 13 '17 at 19:52
  • @PhillipMartin that would probably work as well, ideally I want each product name and price underneath it's corresponding product type – AJK Apr 13 '17 at 19:55
  • 1
    Why three arrays? Use objects! i.e `[{type:bundle,name:prod1,price:1.99},{type:simple,name:prod2,price:2.99}]` – rckrd Apr 13 '17 at 19:55
  • 1
    You can make use of `zip` here, see http://stackoverflow.com/questions/4856717/javascript-equivalent-of-pythons-zip-function – georg Apr 13 '17 at 20:03
  • @georg yes that zip functionality is terribly missing in JS. – Redu Apr 13 '17 at 20:14
  • 1
    Thanks guys! I've updated my question to be a lot more specific, and added the current code that I'm working with. – AJK Apr 13 '17 at 20:51

4 Answers4

2

You can use the combination of map and filter to first combine the three arrays you have and then filter out arrays that match 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));
Phillip Martin
  • 1,910
  • 15
  • 30
1

Yes... JS is missing an Array.prototype.zip() functionality. Let's invent it and solve accordingly.

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: I have swapped the place of the last .map() and .filter() functions to fit your requirement but amending the question yielding changes in the previous answers are not encouraged in SO.

Redu
  • 25,060
  • 6
  • 56
  • 76
0

First, combine them all into an array of objects via map, then filter, then map again into the representation you need. Something like:

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}])
Pavel Veller
  • 6,085
  • 1
  • 26
  • 24
0

You could filter the columns, transpose the arrays and build the wanted inner arrays.

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; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392