Tazh
Tazh

Reputation: 35

Convert strings to numbers in multidimensional array

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.

Upvotes: 0

Views: 1229

Answers (9)

TopMarx
TopMarx

Reputation: 135

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);

Upvotes: 0

Luis Casabona
Luis Casabona

Reputation: 1

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

Upvotes: 0

NYTom
NYTom

Reputation: 524

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>

Upvotes: 0

guest271314
guest271314

Reputation: 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));

Upvotes: 1

DrKey
DrKey

Reputation: 3495

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));

Upvotes: 1

Bladepianist
Bladepianist

Reputation: 490

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

Upvotes: 0

Chazeon
Chazeon

Reputation: 546

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
}

Upvotes: 0

adeneo
adeneo

Reputation: 318182

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

Upvotes: 0

Omri Aharon
Omri Aharon

Reputation: 17064

You can use + to cast strings to numbers:

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

Fiddle

Upvotes: 2

Related Questions