0

I have an object with arrays, each of which has numbers with double quotes, and I'd like to convert it into an array of arrays without double quotes.

This

> Object { CategoryA: Array[182], CategoryB: Array[180],
> CategoryC: Array[182], CategoryD: Array[171],
> CategoryE: Array[182], CategoryF: Array[183] }

to

Array [ Array[182], Array[182], Array[182], Array[182], Array[182], Array[182] ]

I tried .replace(/"/g, ""); but I'm getting that replace is not a function.

JSON.stringify and JSON.parse didn't help me and a for loop

for (var i = 0; i < data.length; i++) {
    data2[i] = data[i].replace(/"/g, "");
}
console.log(data2);

returns double quotes.

UPDATE

This is how my json looks like

{"CategoryA": ["297,239", "277,227", "279,310", "297,766"],
 "CategoryB": ["15,479,207", "14,845,266", "15,454,549"],
 "CategoryC": ["285,648", "295,982", "300,306", "302,508"]
} 

2 Answers 2

6

You can use map in combination with Object.keys like this:

var result = Object.keys(yourObject).sort().map(function(key) {
    return yourObject[key].map(function(num) {
        return +num.replace(/,/g, '');
    });
});

Please note that the comma , in your numbers it's for formatting, not a floating point.

var yourObject = {"CategoryA": ["297,239", "277,227", "279,310", "297,766"],
 "CategoryB": ["15,479,207", "14,845,266", "15,454,549"],
 "CategoryC": ["285,648", "295,982", "300,306", "302,508"]
} ;

var result = Object.keys(yourObject).sort().map(function(key) {
    return yourObject[key].map(function(num) {
      return +num.replace(/,/g, '');
    });
});

console.log(result);

Thank you @Rayon.

Sign up to request clarification or add additional context in comments.

12 Comments

Right. OP: Be careful Object.keys returns the properties of an object in an arbitrary order, so the result of this operation can change over time
What about "without double quotes" ?
Thank you for your reply but as Rayon said this returns double quotes and changes the order. I would prefer if I could keep CategoryA, CategoryB etc. as well.
@jimakos17 an object doesn't have an order. Do you mean alphabetical order?
@IsmailRBOUH – How about ARRAY.map(Number) ?
|
1

You could sort the keys (to maintain alphabetical order) and apply a replace for changing the comma to point and a casting to number.

var object = { "CategoryA": ["297,239", "277,227", "279,310", "297,766"], "CategoryB": ["15,479,207", "14,845,266", "15,454,549"], "CategoryC": ["285,648", "295,982", "300,306", "302,508"] },
    array=  Object.keys(object).sort().map(function(k) {
        return object[k].map(function (a) {
            return +a.replace(/,/g, '');
        });
    });

console.log(array);

2 Comments

Thank you for your answer @Nina but my values are shown NaN with your reply as well.
please add your original object.

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.