简体   繁体   中英

Create object with String index of two arrays in javascript

I have two arrays data=[100,200,300]; inde=[one, two, three];

I want to union this arrays in a object like this:

obj=[{"label":"one","value"=100}, {"label":"two","value"=200}, {"label":"three","value"=300}];

I'm starting with javascript and I can't do it dynamically

var obj = [];
for(var i = 0, len = data.length; i < len; i++){
    var temp = {
        label: inde[i],
        value: data[i]
    };
    obj.push(temp);
}

If data and inde will be the same length you can use sth like this

var data=[100,200,300]; var inde=['one', 'two', 'three'];   
var obj=[];
for(var i=0;i<data.length;i++){
    var tmp={
        label:inde[i],
        value:data[i]
    }
    obj.push(tmp);
}

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