Meek
Meek

Reputation: 3348

Use split in array

I have an object with some values. I would like to remove part of the value while adding the remaining value into a new variable. This is what I have so far:

var data = {
    "selectedProducts": {
        "selectedSubscriptionIds": "320151(Products),320145(Products)"
    }
};
var SelectedSubscriptionIds = data.selectedProducts.selectedSubscriptionIds;
var StrippedSubscriptionIds = SelectedSubscriptionIds.split("(Products)")[0];

console.log(SelectedSubscriptionIds); //320151(Products),320145(Products)
console.log(StrippedSubscriptionIds); //Returns "320151" only

The variable StrippedSubscriptionIds only returns the first stripped value. How do I make it loop through all the items stripping them so that only the numbers are left - like 320151,320145...

Upvotes: 0

Views: 69

Answers (4)

Richard Raby
Richard Raby

Reputation: 106

The split() function outputs an array of items. Instead of selecting the first item through "[0]", assign the whole array to a variable and loop over that, in the format:

for (var count = 0; count < maxCount; count++) {
    // Action to be repeated here
}

A problem you'll have is your source string is in the format "Id(Type),Id(Type)". Splitting on just "(Type)" will leave the commas in the item, so your stripped items would look like "320151" and ",320145". Ensure you include the comma (and make sure your source string has a comma at the end too or your last item won't match the split conditions and will still say "Id(Type)".

So your code would look like:

var data = {
    "selectedProducts": {
        "selectedSubscriptionIds": "320151(Products),320145(Products),"
    }
};
var SelectedSubscriptionIds = data.selectedProducts.selectedSubscriptionIds;
console.log(SelectedSubscriptionIds); //320151(Products),320145(Products),

var subscriptionIdArray = SelectedSubscriptionIds.split("(Products),");

for (var i = 0, i < subscriptionIdArray.length; i++) {
    var subscriptionId = subscriptionIdArray[i];
    console.log(subscriptionId); // 320151, then 320145 etc
}

I'm not sure why you need to specify a subscriptionId as "Id(Type)" when it's already inside the "selectedProducts" category. If it's purely for the sake of splitting then you could leave it out and just split on ","

var data = {
    "selectedProducts": {
        "selectedSubscriptionIds": "320151,320145"
    }
};
var SelectedSubscriptionIds = data.selectedProducts.selectedSubscriptionIds;
console.log(SelectedSubscriptionIds); //320151,320145

var subscriptionIdArray = SelectedSubscriptionIds.split(",");

for (var i = 0, i < subscriptionIdArray.length; i++) {
    var subscriptionId = subscriptionIdArray[i];
    console.log(subscriptionId); // 320151, then 320145 etc
}

Upvotes: 1

fguillen
fguillen

Reputation: 38792

SelectedSubscriptionIds.replace(/\(Products\)/g, "").split(",")

Upvotes: 2

collapsar
collapsar

Reputation: 17238

try

var StrippedSubscriptionIds = SelectedSubscriptionIds.split("(Products),");
StrippedSubscriptionIds[-1] = StrippedSubscriptionIds[-1].substr(0, StrippedSubscriptionIds[-1].length - 10);

and you have an array.

the last code line eliminates the trailing (Product) string.

alternatively, use:

var temp = SelectedSubscriptionIds.replace(/\(Products\)/g, "");
var StrippedSubscriptionIds = temp.split(",");

Upvotes: 0

Malcor
Malcor

Reputation: 2721

foreach id in StrippedSubscriptionIds

Console.log(id);

Next

Upvotes: 0

Related Questions