简体   繁体   中英

Combining two arrays into one javascript json object

I have two arrays:

var itemname = [ "t-shirt", "trouser", "vest" ];
var itemqty = [ "3", "5", "2" ];

I want to combine them into:

var keyval = [{
    itemname: "trouser", 
    itemqty: 5
}, {
    itemname: "vest, 
    itemqty: 2
},{
    itemname: "t-shirt", 
    itemqty: 3
}]

How can I achieve this using javascript or jquery?

var keyVal = [];
for (var i = 0; i < itemname.length; i++)
{
    keyVal.push({ itemname: itemname[i], itemqty: itemqty[i] });
}

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