将两个数组合并到JSON中的对象中

时间:2014-08-18 20:53:12

标签: javascript arrays json object lodash

column_names: [
    "School Year Ending",
    "Total Students",
    "American Indian/Alaskan Native: Total",
    "American Indian/Alaskan Native: Male",
    "American Indian/Alaskan Native: Female",
    "Asian/Pacific Islander: Total",
    "Asian/Pacific Islander: Male",
    "Asian/Pacific Islander: Female",
    "Hispanic: Total",
    "Hispanic: Male",
    "Hispanic: Female",
    "Black: Total",
    "Black: Male",
    "Black: Female",
    "White: Total",
    "White: Male",
    "White: Female"
],
data: [
    "2011-12-31",
    3471888,
    39433,
    19926,
    19507,
    182697,
    94104,
    88593,
    686081,
    344910,
    341171,
    536271,
    260720,
    275551,
    1970983,
    1006592,
    964391
]

有没有办法将这两个JSON数组转换为对象格式?例如:

"School Year Ending": "2011-12-31",
"Total Students": 3721888,
...

为方便起见,我更喜欢使用lo-dash功能。有这样的事吗?

4 个答案:

答案 0 :(得分:3)

_.zipObject(column_names, data);

应该这样做。

答案 1 :(得分:0)

是。我相信这会用普通的JS来做。

var columns = { };
for (var i in column_names) 
    columns[column_names[i]] = data[i];

JSFiddle

答案 2 :(得分:0)

使用vanilla JavaScript - Fiddle

var column_names = [
    "School Year Ending",
    "Total Students",
    "American Indian/Alaskan Native: Total",
    "American Indian/Alaskan Native: Male",
    "American Indian/Alaskan Native: Female",
    "Asian/Pacific Islander: Total",
    "Asian/Pacific Islander: Male",
    "Asian/Pacific Islander: Female",
    "Hispanic: Total",
    "Hispanic: Male",
    "Hispanic: Female",
    "Black: Total",
    "Black: Male",
    "Black: Female",
    "White: Total",
    "White: Male",
    "White: Female"
],
data = [
    "2011-12-31",
    3471888,
    39433,
    19926,
    19507,
    182697,
    94104,
    88593,
    686081,
    344910,
    341171,
    536271,
    260720,
    275551,
    1970983,
    1006592,
    964391
],
obj = {};

for (var i = 0, l = column_names.length; i < l; i += 1) {
    obj[column_names[i]] = data[i]
}
console.log(obj);

答案 3 :(得分:0)

又有香草JS的另一种解决方案!这使用forEach

http://jsfiddle.net/w5f36jwd/

school_data = {
    column_names:
    [
        "School Year Ending",
        "Total Students",
        "American Indian/Alaskan Native: Total",
        "American Indian/Alaskan Native: Male",
        "American Indian/Alaskan Native: Female",
        "Asian/Pacific Islander: Total",
        "Asian/Pacific Islander: Male",
        "Asian/Pacific Islander: Female",
        "Hispanic: Total",
        "Hispanic: Male",
        "Hispanic: Female",
        "Black: Total",
        "Black: Male",
        "Black: Female",
        "White: Total",
        "White: Male",
        "White: Female"
    ],
    data:
    [
        "2011-12-31",
        3471888,
        39433,
        19926,
        19507,
        182697,
        94104,
        88593,
        686081,
        344910,
        341171,
        536271,
        260720,
        275551,
        1970983,
        1006592,
        964391
    ]
}

var new_object = {};

school_data.column_names.forEach(function(el, index, data_array){
    new_object[el] = school_data.data[index];
});

console.log(new_object);