简体   繁体   中英

How can i put loop and get values in javascript?

<script>
var data={ 
    Data: { 
        name: 'aaaa',
        number: '0003'
    },
    values: { 
        val: '-20.00',
        rate: '22047' 
    },
    user: [ '6|1|5', '10|1|15' ] 
};

console.log(data);
console.log(data.user.length);
for(var i=0;i<data.user.length;i++) {
    console.log(data.user[i]);
}
</script>

Above is my code i want to put loop and get values like this

this is my data user: [ '6|1|5', '10|1|15' ]

but i want to get like this:

  • userid - 6
  • roolno - 1
  • rank - 5


  • userid - 10

  • roolno - 1
  • rank - 15

how can i do this any one help me out ?

A simple map would do it ( see plunker ):

data.user.map(function(x) { 
    var parts = x.split('|');
    return {
        userid: parts[0],
        roolno: parts[1],
        rank: parts[2]
    };
});

This would return you:

[
    {
        userid: 1,
        roolno: 1,
        rank: 5
    },
    {
        userid: 10,
        roolno: 1,
        rank: 15
    },
]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM