-2

I have 3 strings that I need to convert into a single array, from there I want to filter out the type: "bundle".

I need to note that I'm using Javascript Code by Zapier and their javascript library is a bit limited as far as the functions that I can use, but this is what I have so far which works if I hard code itemArray. I'm just having trouble creating my itemArray from the 3 given strings:

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 return the simple product types, I'm doing that with the following code:

// 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
  • how is this question different from the dupe target? – Nina Scholz Apr 13 '17 at 21:31
  • I meant to delete that dupe, because I majorly edited the question and had answers that were no longer related. sorry still learning the rules around here – AJK Apr 13 '17 at 21:33
  • 1
    maybe you take some more care of how to ask a question, i for example missed the right flow, what you have, what you want and what you have tried. the data structure has changed, the filtering is not really well displayed and now, you refuse to select an answer of being part of the solution. but morping question makes answering hard. – Nina Scholz Apr 13 '17 at 21:39
  • 2
    I agree, should've taken more time to correctly ask my question. I'll be more careful going forward! I appreciate your input – AJK Apr 13 '17 at 21:43

2 Answers2

1

First make the strings into arrays of the three strings you want. Then in a for loop you can push all of them in whatever (identical) format you want, since all 3 lists have 3 elements each. then you can use the filter function to easily filter out the bundle elements as below. The following snippet will print out the item array and the filtered value you requested

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});
Matthew Ciaramitaro
  • 1,184
  • 1
  • 13
  • 27
1

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);
kind user
  • 40,029
  • 7
  • 67
  • 77