0

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

1
  • You have 0 index at the end of the statement which returns the first element of the array, remove that and t will work fine. Commented Aug 20, 2013 at 11:27

4 Answers 4

2
SelectedSubscriptionIds.replace(/\(Products\)/g, "").split(",")
Sign up to request clarification or add additional context in comments.

1 Comment

@fguillen That's neat! Works like a charm. Thanks!
1

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
}

Comments

0
foreach id in StrippedSubscriptionIds

Console.log(id);

Next

Comments

0

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(",");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.