Javascript 将数组值转换为对象键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43137010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert array values to object keys
提问by protoskull
I do a get which returns me a json object like so:
我做了一个 get 返回一个 json 对象,如下所示:
"data": [
[
"2016 Pass/Fail Rates by Test Centre",
"",
"",
"",
"",
"",
"",
"",
"",
""
],
[
"",
"Passes",
"",
"No ID",
"",
"Fails",
"",
"Fail Dangerous",
"",
"Total"
],
[
"Sometown",
"8,725",
"53.40%",
"140",
"0.90%",
"7,417",
"45.40%",
"48",
"0.30%",
"16,330"
],
[
"Some Other Town",
"12,778",
"44.80%",
"193",
"0.70%",
"15,422",
"54.10%",
"103",
"0.40%",
"28,496"
],
[... many more identically formatted arrays ...]
and I would like to end up with:
我想结束:
[{"Location":"Sometown", "Passes":8,725, "Pass%":53.40%, "No ID":140, "NoID%":0.90%, "Fails":7,417, "Fail%":45.40%, "Fail Dangerous":48, "FailDangerous%":0.30%, "Total":16,330}, {"Location":"Some Other Town", "Passes":8,725, etc etc...
So I want to ignore the first array inside the "data" array, use the values of the second array as keys (and replace the empty strings with something more useful) and the values in all remaining arrays as values in the resulting object.
所以我想忽略“数据”数组中的第一个数组,使用第二个数组的值作为键(并用更有用的东西替换空字符串)和所有剩余数组中的值作为结果对象中的值。
So it is quite the multipart problem, though I suspect a fairly simple one. What is the simplest way to do that - and if different, what is the leanest way to do it in terms of processing/page load?
所以这是一个相当多的问题,尽管我怀疑这是一个相当简单的问题。最简单的方法是什么 - 如果不同,在处理/页面加载方面最精简的方法是什么?
Thanks in advance,
提前致谢,
采纳答案by Nina Scholz
You could take the second array as keys for the wanted objects and iterate only the part after the keys. Then iterate the keys and build a new object for the values of the array. Return the object for mapping for a new array.
您可以将第二个数组作为所需对象的键,并仅迭代键之后的部分。然后迭代键并为数组的值构建一个新对象。返回用于映射新数组的对象。
var data = { data: [["2016 Pass/Fail Rates by Test Centre", "", "", "", "", "", "", "", "", ""], ["Location", "Passes", "Passes%", "No ID", "No ID%", "Fails", "Fails%", "Fail Dangerous", "Fail Dangerous%", "Total"], ["Sometown", "8,725", "53.40%", "140", "0.90%", "7,417", "45.40%", "48", "0.30%", "16,330"], ["Some Other Town", "12,778", "44.80%", "193", "0.70%", "15,422", "54.10%", "103", "0.40%", "28,496"]] },
keys = data.data[1],
result = data.data.slice(2).map(function (a) {
var temp = {};
keys.forEach(function (k, i) {
temp[k] = a[i];
})
return temp;
});
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
回答by McKabue
You can iterate over your array while adding your index and value into an object...
您可以在将索引和值添加到对象中的同时遍历数组...
You can find an example on how you can iterate objects and arrays hereand an example here
Generally, to add an item to object
var array = [{"Location":"Sometown"}, {"Location2":"Sometown2"}, {"Location3":"Sometown3"}],
object = {};
array.forEach(function(element, index) {
object[index] = element;
});
console.log(object);
回答by McKabue
Here is my solution, hoping it is useful to you & anybody who has the same confused with this problem!
这是我的解决方案,希望它对您和任何对此问题感到困惑的人有用!
- you should construct a meaningful object!
- 你应该构造一个有意义的对象!
const meaningful_objs = {
"Location": "",
"Passes": "",
"Passes%": "",
"No ID": "",
"No ID%": "",
"Fails": "",
"Fails%": "",
"Fail Dangerous": "",
"Fail Dangerous%": "",
"Total": ""
};
// or get it from you data[1],
// but I think it has some errors of you got returned data!
// It should be have all keys!
// just make a assume, you get the right data[1]!
const data[1] = ["Location", "Passes", "Passes%", "No ID", "No ID%", "Fails", "Fails%", "Fail Dangerous", "Fail Dangerous%", "Total"];
let keys = data[1];
/*
Array to Object
*/
// ["Location", "Passes", "Passes%", "No ID", "No ID%", "Fails", "Fails%", "Fail Dangerous", "Fail Dangerous%", "Total"]
const keys_array = [
"Location",
"Passes",
"Passes%",
"No ID",
"No ID%",
"Fails",
"Fails%",
"Fail Dangerous",
"Fail Dangerous%",
"Total"
];
let temp_obj = {};
keys_array.map(
(value, index) => {
temp_obj[`${value}`] = "";
// temp_obj[value] = "";
return temp_obj;
}
);
console.log(`temp_obj = `, temp_obj);
// {"Location": "","Passes": "","Passes%": "","No ID": "","No ID%": "", "Fails": "","Fails%": "","Fail Dangerous": "","Fail Dangerous%": "","Total": ""};
for(let k in temp_obj) {
console.log(`typeof (k) = `, typeof (k));
// typeof (k) = string
}
/*
Object to Array
*/
const meaningful_objs = {
"Location": "",
"Passes": "",
"Passes%": "",
"No ID": "",
"No ID%": "",
"Fails": "",
"Fails%": "",
"Fail Dangerous": "",
"Fail Dangerous%": "",
"Total": ""
};
// {"Location": "","Passes": "","Passes%": "","No ID": "","No ID%": "", "Fails": "","Fails%": "","Fail Dangerous": "","Fail Dangerous%": "","Total": ""};
let keys_array = Object.keys(meaningful_objs);
console.log(`keys_array = \n`, keys_array);
// ["Location", "Passes", "Passes%", "No ID", "No ID%", "Fails", "Fails%", "Fail Dangerous", "Fail Dangerous%", "Total"]
/*
result
*/
let keys = keys_array;
// ["Location", "Passes", "Passes%", "No ID", "No ID%", "Fails", "Fails%", "Fail Dangerous", "Fail Dangerous%", "Total"]
let arrays = [
[
"Sometown",
"8,25",
"53.40%",
"140",
"0.90%",
"7,17",
"45.40%",
"48",
"0.30%",
"16,30"
],
[
"Some Other Town",
"12,78",
"44.80%",
"193",
"0.70%",
"15,22",
"54.10%",
"103",
"0.40%",
"28,96"
]
];
let result = arrays.map(
(array) => {
let temp = {};
keys.forEach(
(key, index) => {
console.log(`key = `, key);
console.log(`index = `, index);
temp[`${key}`] = array[index];
}
);
console.log(`temp = `, temp);
return temp;
}
);
console.log(`result = `, result);
/*
test
*/
let string_objs = JSON.stringify(result);
// "[{"Location":"Sometown","Passes":"8,25","Passes%":"53.40%","No ID":"140","No ID%":"0.90%","Fails":"7,17","Fails%":"45.40%","Fail Dangerous":"48","Fail Dangerous%":"0.30%","Total":"16,30"},{"Location":"Some Other Town","Passes":"12,78","Passes%":"44.80%","No ID":"193","No ID%":"0.70%","Fails":"15,22","Fails%":"54.10%","Fail Dangerous":"103","Fail Dangerous%":"0.40%","Total":"28,96"}]"
let string_obj1 = JSON.stringify(result[0]);
// "{"Location":"Sometown","Passes":"8,25","Passes%":"53.40%","No ID":"140","No ID%":"0.90%","Fails":"7,17","Fails%":"45.40%","Fail Dangerous":"48","Fail Dangerous%":"0.30%","Total":"16,30"}"
/*
{
"Location":"Sometown",
"Passes":"8,25",
"Passes%":"53.40%",
"No ID":"140",
"No ID%":"0.90%",
"Fails":"7,17",
"Fails%":"45.40%",
"Fail Dangerous":"48",
"Fail Dangerous%":"0.30%",
"Total":"16,30"
}
*/
let string_obj2 = JSON.stringify(result[1]);
// "{"Location":"Some Other Town","Passes":"12,78","Passes%":"44.80%","No ID":"193","No ID%":"0.70%","Fails":"15,22","Fails%":"54.10%","Fail Dangerous":"103","Fail Dangerous%":"0.40%","Total":"28,96"}"
/*
{
"Location":"Some Other Town",
"Passes":"12,78",
"Passes%":"44.80%",
"No ID":"193",
"No ID%":"0.70%",
"Fails":"15,22",
"Fails%":"54.10%",
"Fail Dangerous":"103",
"Fail Dangerous%":"0.40%",
"Total":"28,96"
}
*/

