开发者

Javascript array creation

开发者 https://www.devze.com 2023-02-23 06:33 出处:网络
How do I turn this string: house,33;car,43;dog\'s,99; into this array: arr[33]=\"house\" arr[43]=\"car\" arr[99]=\"dog\'s\" using javascript and jquery.

How do I turn this string: house,33;car,43;dog's,99; into this array: arr[33]="house" arr[43]="car" arr[99]="dog's" using javascript and jquery.

Once I have the array, can开发者_如何学Go I store information (like a 0 or 1 flag) next to each?


try this..

var initString = "house,33;car,43;dog's,99";
var array1 = initString.split(';')
var result = [];
    for(var i=0,l=array1.length;i<l;i++){
        var items = array1[i].split(',');
        result[parseInt(items[1])] = {flag:0, value:items[0]};
    }


var str = "house,33;car,43;dog's,99;";
var pieces = str.split(';');
var arr = new Array();
for (var index = 0; index < pieces.length; index++) {
    var halves = pieces[index].split(',');
    arr[ halves[1] ] = halves[0];
}

Though you really shouldn't be specifying the array index unless you have a complete list from 0 to 99 (your sample has only three entries, all of which are well into that sequence).

0

精彩评论

暂无评论...
验证码 换一张
取 消