根据键名组织对象 - Underscore.js
Organizing Object based on key name - Underscore.js
正在寻找获取此对象的方法
var config = {
"F2-1_largerLegal-input": "F2-1_largerLegal-input",
"F2-1_copy-font": "Ultra",
"F2-3_copy-fontSize": "12",
"F2-1_copy-input": "F2-1_copy-input",
"F2-1_frameLegal-input": "Ultra",
"F2-1_frameLegal-fontSize": "14",
"F2-2_copy-input": "F2-2_copy-input",
"F2-3_copy-input": "F2-3_copy-input",
"F2-3_copy-font": "Medium",
"F2-1_copy-fontSize": "10",
"F2-1_product-src": "250/50/F2-1_product.png",
"F2-2_copy-font": "Medium",
"F2-2_copy-fontSize": "11",
"F2-1_largerLegal-fontSize": "13"
};
并使用 'underscore.js'.
将其转换为如下所示的对象结构
var config = {
"F2":{
"copy":{
"F2-1_copy-input": "F2-1_copy-input",
"F2-1_copy-font": "Ultra",
"F2-3_copy-fontSize": "12",
"F2-2_copy-input": "F2-2_copy-input",
"F2-3_copy-input": "F2-3_copy-input",
"F2-3_copy-font": "Medium",
"F2-1_copy-fontSize": "10",
"F2-2_copy-font": "Medium",
"F2-2_copy-fontSize": "11"
},
"frameLegal": {
"F2-1_frameLegal-input": "Ultra",
"F2-1_frameLegal-fontSize": "14"
},
"largerLegal":{
"F2-1_largerLegal-input": "F2-1_largerLegal-input",
"F2-1_largerLegal-fontSize": "13"
}
"src": {
"F2-1_product-src": "250/50/F2-1_product.png",
}
}
};
他们是循环对象和返回对象的最佳实践吗?每当我尝试对象的值为空时。
利用JavaScript的reduce
功能怎么样?它会为您解决问题。
var config = { "F2-1_largerLegal-input": "F2-1_largerLegal-input", "F2-1_copy-font": "Ultra", "F2-3_copy-fontSize": "12", "F2-1_copy-input": "F2-1_copy-input", "F2-1_frameLegal-input": "Ultra", "F2-1_frameLegal-fontSize": "14", "F2-2_copy-input": "F2-2_copy-input", "F2-3_copy-input": "F2-3_copy-input", "F2-3_copy-font": "Medium", "F2-1_copy-fontSize": "10", "F2-1_product-src": "250/50/F2-1_product.png", "F2-2_copy-font": "Medium", "F2-2_copy-fontSize": "11", "F2-1_largerLegal-fontSize": "13"};
result = Object.entries(config).reduce((acc, [key, value])=>{
const [keyInput] = key.split("-");
const [nestedKey] = key.split("_")[1].split('-');
acc[keyInput] = acc[keyInput] || {};
acc[keyInput][nestedKey] = acc[keyInput][nestedKey] || {};
acc[keyInput][nestedKey] = Object.assign(acc[keyInput][nestedKey], {[key]:value});
return acc;
},{});
console.log(result);
正在寻找获取此对象的方法
var config = {
"F2-1_largerLegal-input": "F2-1_largerLegal-input",
"F2-1_copy-font": "Ultra",
"F2-3_copy-fontSize": "12",
"F2-1_copy-input": "F2-1_copy-input",
"F2-1_frameLegal-input": "Ultra",
"F2-1_frameLegal-fontSize": "14",
"F2-2_copy-input": "F2-2_copy-input",
"F2-3_copy-input": "F2-3_copy-input",
"F2-3_copy-font": "Medium",
"F2-1_copy-fontSize": "10",
"F2-1_product-src": "250/50/F2-1_product.png",
"F2-2_copy-font": "Medium",
"F2-2_copy-fontSize": "11",
"F2-1_largerLegal-fontSize": "13"
};
并使用 'underscore.js'.
将其转换为如下所示的对象结构var config = {
"F2":{
"copy":{
"F2-1_copy-input": "F2-1_copy-input",
"F2-1_copy-font": "Ultra",
"F2-3_copy-fontSize": "12",
"F2-2_copy-input": "F2-2_copy-input",
"F2-3_copy-input": "F2-3_copy-input",
"F2-3_copy-font": "Medium",
"F2-1_copy-fontSize": "10",
"F2-2_copy-font": "Medium",
"F2-2_copy-fontSize": "11"
},
"frameLegal": {
"F2-1_frameLegal-input": "Ultra",
"F2-1_frameLegal-fontSize": "14"
},
"largerLegal":{
"F2-1_largerLegal-input": "F2-1_largerLegal-input",
"F2-1_largerLegal-fontSize": "13"
}
"src": {
"F2-1_product-src": "250/50/F2-1_product.png",
}
}
};
他们是循环对象和返回对象的最佳实践吗?每当我尝试对象的值为空时。
利用JavaScript的reduce
功能怎么样?它会为您解决问题。
var config = { "F2-1_largerLegal-input": "F2-1_largerLegal-input", "F2-1_copy-font": "Ultra", "F2-3_copy-fontSize": "12", "F2-1_copy-input": "F2-1_copy-input", "F2-1_frameLegal-input": "Ultra", "F2-1_frameLegal-fontSize": "14", "F2-2_copy-input": "F2-2_copy-input", "F2-3_copy-input": "F2-3_copy-input", "F2-3_copy-font": "Medium", "F2-1_copy-fontSize": "10", "F2-1_product-src": "250/50/F2-1_product.png", "F2-2_copy-font": "Medium", "F2-2_copy-fontSize": "11", "F2-1_largerLegal-fontSize": "13"};
result = Object.entries(config).reduce((acc, [key, value])=>{
const [keyInput] = key.split("-");
const [nestedKey] = key.split("_")[1].split('-');
acc[keyInput] = acc[keyInput] || {};
acc[keyInput][nestedKey] = acc[keyInput][nestedKey] || {};
acc[keyInput][nestedKey] = Object.assign(acc[keyInput][nestedKey], {[key]:value});
return acc;
},{});
console.log(result);