0

I have an array

MyArray = [ 
["Oswaldo","21749"],
["Mao","19580"],
["Angeline",'53692']
]

How do I convert the numbers from strings to actual numbers and save in the same array or in the new one.

9 Answers 9

2

You can use + to cast strings to numbers:

for (var i in myArray) {
    myArray[i][1] = +myArray[i][1];
}

Fiddle

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

Comments

1

I think Array map is more efficient and elegant:

var MyArray = [ 
["Oswaldo","21749"],
["Mao","19580"],
["Angeline",'53692']
];

MyArray = MyArray.map(function (obj) {
    return [obj[0],parseInt(obj[1])];
});

document.write(JSON.stringify(MyArray));

Comments

1

Note, js attempts not referencing an index , for instance where position of array item string containing only numbers may not be at index 1 within an array contained within MyArray

Try

var MyArray = [
  ["Oswaldo", "21749"],
  ["Mao", "19580"],
  ["Angeline", '53692']
];

MyArray = MyArray.map(function(items) {
  return items.map(function(item) {
    return /^\d+$/.test(item) && !/[a-z]/i.test(item) ? Number(item) : item
  })
});

console.log(JSON.stringify(MyArray));

3 Comments

We answer at the same time, but you can achieve that with just one map. Watch my answer..
@DrKey See updated post. js attempts to not reference at specific index within array within MyArray , for instance where array item containing numbers may not be at index 1 within array within MyArray
Of course my function will works only if array within MyArray contains 2 values with original index. Your function works even if that array contains more than two values at random indexes. Upvoted!
0

Recursive iteration will let you iterate nested array, then just coherce the value to Number

function parseNum(arr) {

    arr.forEach(function(value, index) {
        if ( Array.isArray(value) ) {
            parseNum(value);
        } else if ( !isNaN(parseFloat(value)) ) {
            arr[index] = (+value);
        }
    });

}

FIDDLE

Comments

0

You can use parseInt() or parseFloat() function.

for (var i = 0; i < MyArray.length; ++i) {
    MyArray[i][1] = parseInt(MyArray[i][1]);
    // Or parseFloat if there is a float
}

Comments

0

http://jsfiddle.net/Bladepianist/rpy2aedh/1/

MyArray = [ 
["Oswaldo","21749"],
["Mao","19580"],
["Angeline",'53692']
];

for (var item in MyArray) {
    MyArray[item][1] = parseInt(item[1], 10);
}

parseInt() if you want them in int and the "10" is for base 10. parseFloat() if you'd prefer them in that format :).

Comments

0

Using the open source project jinqJs

var MyArray = [ 
["Oswaldo","21749"],
["Mao","19580"],
["Angeline",'53692']
];

var result = jinqJs().from(MyArray).select(function(row){row[1] = Number(row[1]); return row;});
document.body.innerHTML += '<pre>' + JSON.stringify(result, null, 4) + '</pre><br><br>';
<script src="https://rawgit.com/fordth/jinqJs/master/jinqjs.js"></script>

Comments

0

Changes to the previous example

var MyArray = [ 
   [0, "Oswaldo","21749"],
   [1, "Mao","19580"],
   [2, "Angeline",'53692']
];

function buscar() {
  var result = new jinqJs().from(MyArray).where(function(row){return (row[0] == '1')}).select(function(row){return row[1]});
  console.log(result);
}

Plunker

Comments

0

Two variations provided below:

The first for changing a specific index in a 2D array to numbers, as per the OP.

The second for converting every element of an array of number strings to numbers, which is the answer I was looking for when I found this page.

const stringArr = [ 
    ["Oswaldo","21749"],
    ["Mao","19580"],
    ["Angeline","53692"]
  ];

stringArr.forEach((el,i,arr) => arr[i][1] = +el[1]); // convert every index[1] of 2D array to number

console.log(stringArr);

const numStringArr = [ 
    ["31749","21749"],
    ["29580","19580"],
    ["63692","53692"]
  ];

numStringArr.forEach((el,i,arr) => arr[i] = el.map(e => +e)); // convert every element of 2D array to number

console.log(numStringArr);

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.