js数据处理
const translations = {
'editor.remove': 'Remove',
'editor.copy': 'Copy',
'editor.words': 'WORDS',
'editor.characters': 'CHARACTERS',
'editor.default': 'Default',
'editor.recent': 'Recently Used',
'editor.nofill': 'Remove Color',
'editor.format': 'Format Painter',
'editor.draghandle.tooltip': 'Click And Drag',
'editor.copyToClipboard': 'CopyToClipboard',
'editor.importWrod.tooltip': 'Import Word',
'editor.slash': "Press '/' for commands",
'editor.slash.empty': 'No Result',
'editor.slash.format': 'Format',
'editor.slash.insert': 'Insert',
'editor.slash.embed': 'Embed Services',
'editor.content': 'Please input content',
'editor.moremark': 'More Text Styles',
'editor.size.small.tooltip': 'Small',
'editor.size.medium.tooltip': 'Medium',
'editor.size.large.tooltip': 'Cover',
'editor.bold.tooltip': 'Bold',
'editor.italic.tooltip': 'Italic',
'editor.underline.tooltip': 'Underline',
'editor.strike.tooltip': 'Strike',
'editor.color.tooltip': 'Color',
'editor.color.more': 'Color Picker',
'editor.highlight.tooltip': 'Highlight',
'editor.lineheight.tooltip': 'Line Height',
'editor.heading.tooltip': 'Headings',
'editor.heading.h1.tooltip': 'Heading 1',
'editor.heading.h2.tooltip': 'Heading 2',
'editor.heading.h3.tooltip': 'Heading 3',
'editor.heading.h4.tooltip': 'Heading 4',
'editor.heading.h5.tooltip': 'Heading 5',
'editor.heading.h6.tooltip': 'Heading 6',
'editor.paragraph.tooltip': 'Paragraph',
'editor.textalign.tooltip': 'Align',
'editor.textalign.left.tooltip': 'Left',
'editor.textalign.center.tooltip': 'Center',
'editor.textalign.right.tooltip': 'Right',
'editor.textalign.justify.tooltip': 'Justify',
'editor.indent': 'Indent',
'editor.indent.indent': 'Increase Indent',
'editor.indent.outdent': 'Decrease Indent',
'editor.fontFamily.tooltip': 'Font family',
'editor.fontSize.tooltip': 'Font size',
'editor.fontSize.default.tooltip': 'Default',
'editor.superscript.tooltip': 'Superscript',
'editor.subscript.tooltip': 'Subscript',
'editor.bulletlist.tooltip': 'Bullet List',
'editor.orderedlist.tooltip': 'Ordered List',
'editor.tasklist.tooltip': 'Task List',
'editor.indent.tooltip': 'Indent',
'editor.outdent.tooltip': 'Outdent',
'editor.columns.tooltip': 'Columns',
'editor.link.tooltip': 'Link',
'editor.link.unlink.tooltip': 'UnLink',
'editor.link.open.tooltip': 'Open Link',
'editor.link.edit.tooltip': 'Edit Link',
'editor.link.dialog.title': 'Insert Link',
'editor.link.dialog.link': 'Link',
'editor.link.dialog.text': 'Text',
'editor.link.dialog.openInNewTab': 'Open In New Tab',
'editor.link.dialog.link.placeholder': 'Link Address',
'editor.link.dialog.text.placeholder': 'Add Description',
'editor.link.dialog.button.apply': 'Apply',
'editor.image.tooltip': 'Image',
'editor.image.dragger.tooltip': 'Click or drag the image to the area to upload',
'editor.image.float.left.tooltip': 'Float left',
'editor.image.float.none.tooltip': 'Float none',
'editor.image.float.right.tooltip': 'Float right',
'editor.image.dialog.title': 'Add an image',
'editor.image.dialog.tab.url': 'Url',
'editor.image.dialog.tab.upload': 'Upload',
'editor.image.dialog.uploading': 'Uploading',
'editor.image.dialog.form.link': 'Link',
'editor.image.dialog.placeholder': 'Link',
'editor.image.dialog.form.alt': 'Alt',
'editor.image.dialog.form.aspectRatio': 'Lock original aspect ratio',
'editor.image.dialog.form.file': 'File',
'editor.image.dialog.button.apply': 'apply',
'editor.video.tooltip': 'Video',
'editor.video.dialog.tab.upload': 'Upload',
'editor.video.dialog.uploading': 'Uploading',
'editor.video.dialog.title': 'Embed or upload a video',
'editor.video.dialog.link': 'link',
'editor.video.dialog.placeholder': 'Link',
'editor.video.dialog.button.apply': 'apply',
'editor.table.tooltip': 'Table',
'editor.table.menu.insert_table': 'Insert Table',
'editor.table.menu.insert_table.with_header_row': 'With header row',
'editor.table.menu.insertColumnBefore': 'Insert Column Before',
'editor.table.menu.insertColumnAfter': 'Insert Column After',
'editor.table.menu.deleteColumn': 'Delete Column',
'editor.table.menu.insertRowAbove': 'Insert Row Above',
'editor.table.menu.insertRowBelow': 'Insert Row Below',
'editor.table.menu.deleteRow': 'Delete Row',
'editor.table.menu.mergeCells': 'Merge Cells',
'editor.table.menu.splitCells': 'Split Cells',
'editor.table.menu.deleteTable': 'Delete Table',
'editor.table.menu.setCellsBgColor': 'Cell Background Color',
'editor.blockquote.tooltip': 'Blockquote',
'editor.horizontalrule.tooltip': 'Horizontal Rule',
'editor.code.tooltip': 'Code',
'editor.codeblock.tooltip': 'Code Block',
'editor.clear.tooltip': 'Clear Format',
'editor.undo.tooltip': 'Undo',
'editor.redo.tooltip': 'Redo',
'editor.fullscreen.tooltip.fullscreen': 'Fullscreen',
'editor.fullscreen.tooltip.exit': 'Fullscreen Exit',
'editor.imageUpload.fileTypeNotSupported': 'File type not supported',
'editor.imageUpload.fileSizeTooBig': 'File size too big, Maximum size is'
};
如何使用js转换
const translations = {
editor: {
remove: 'Remove',
copy: 'Copy',
words: 'WORDS',
characters: 'CHARACTERS',
default: 'Default',
recent: 'Recently Used',
nofill: 'Remove Color',
format: 'Format Painter',
draghandle: {
tooltip: 'Click And Drag'
},
copyToClipboard: 'CopyToClipboard',
importWrod: {
tooltip: 'Import Word'
},
slash: {
default: "Press '/' for commands",
empty: 'No Result',
format: 'Format',
insert: 'Insert',
embed: 'Embed Services'
},
content: 'Please input content',
moremark: 'More Text Styles',
size: {
small: {
tooltip: 'Small'
},
medium: {
tooltip: 'Medium'
},
large: {
tooltip: 'Cover'
}
},
bold: {
tooltip: 'Bold'
},
italic: {
tooltip: 'Italic'
},
underline: {
tooltip: 'Underline'
},
strike: {
tooltip: 'Strike'
},
color: {
tooltip: 'Color',
more: 'Color Picker'
},
highlight: {
tooltip: 'Highlight'
},
lineheight: {
tooltip: 'Line Height'
},
heading: {
tooltip: 'Headings',
h1: {
tooltip: 'Heading 1'
},
h2: {
tooltip: 'Heading 2'
},
h3: {
tooltip: 'Heading 3'
},
h4: {
tooltip: 'Heading 4'
},
h5: {
tooltip: 'Heading 5'
},
h6: {
tooltip: 'Heading 6'
}
},
paragraph: {
tooltip: 'Paragraph'
},
textalign: {
tooltip: 'Align',
left: {
tooltip: 'Left'
},
center: {
tooltip: 'Center'
},
right: {
tooltip: 'Right'
},
justify: {
tooltip: 'Justify'
}
},
indent: 'Indent',
indent: {
indent: 'Increase Indent',
outdent: 'Decrease Indent'
},
fontFamily: {
tooltip: 'Font family'
},
fontSize: {
tooltip: 'Font size',
default: {
tooltip: 'Default'
}
},
superscript: {
tooltip: 'Superscript'
},
subscript: {
tooltip: 'Subscript'
},
bulletlist: {
tooltip: 'Bullet List'
},
orderedlist: {
tooltip: 'Ordered List'
},
tasklist: {
tooltip: 'Task List'
},
indent: {
tooltip: 'Indent'
},
outdent: {
tooltip: 'Outdent'
},
columns: {
tooltip: 'Columns'
},
link: {
tooltip: 'Link',
unlink: {
tooltip: 'UnLink'
},
open: {
tooltip: 'Open Link'
},
edit: {
tooltip: 'Edit Link'
},
dialog: {
title: 'Insert Link',
link: 'Link',
text: 'Text',
openInNewTab: 'Open In New Tab',
link: {
placeholder: 'Link Address'
},
text: {
placeholder: 'Add Description'
},
button: {
apply: 'Apply'
}
}
},
image: {
tooltip: 'Image',
dragger: {
tooltip: 'Click or drag the image to the area to upload'
},
float: {
left: {
tooltip: 'Float left'
},
none: {
tooltip: 'Float none'
},
right: {
tooltip: 'Float right'
}
},
dialog: {
title: 'Add an image',
tab: {
url: 'Url',
upload: 'Upload'
},
uploading: 'Uploading',
form: {
link: 'Link',
placeholder: 'Link',
alt: 'Alt',
aspectRatio: 'Lock original aspect ratio',
file: 'File'
},
button: {
apply: 'apply'
}
}
},
video: {
tooltip: 'Video',
dialog: {
tab: {
upload: 'Upload'
},
uploading: 'Uploading',
title: 'Embed or upload a video',
link: 'link',
placeholder: 'Link',
button: {
apply: 'apply'
}
}
},
table: {
tooltip: 'Table',
menu: {
insert_table: 'Insert Table',
with_header_row: 'With header row',
insertColumnBefore: 'Insert Column Before',
insertColumnAfter: 'Insert Column After',
deleteColumn: 'Delete Column',
insertRowAbove: 'Insert Row Above',
insertRowBelow: 'Insert Row Below',
deleteRow: 'Delete Row',
mergeCells: 'Merge Cells',
splitCells: 'Split Cells',
deleteTable: 'Delete Table',
setCellsBgColor: 'Cell Background Color'
}
},
blockquote: {
tooltip: 'Blockquote'
},
horizontalrule: {
tooltip: 'Horizontal Rule'
},
code: {
tooltip: 'Code'
},
codeblock: {
tooltip: 'Code Block'
},
clear: {
tooltip: 'Clear Format'
},
undo: {
tooltip: 'Undo'
},
redo: {
tooltip: 'Redo'
},
fullscreen: {
tooltip: {
fullscreen: 'Fullscreen',
exit: 'Fullscreen Exit'
}
},
imageUpload: {
fileTypeNotSupported: 'File type not supported',
fileSizeTooBig: 'File size too big, Maximum size is'
}
}
};
const translations = {
'editor.imageUpload.fileSizeTooBig': 'File size too big, Maximum size is'
};
function trans(data) {
const res = {};
for (const keyStr of Object.keys(data)) {
const keys = keyStr.split('.');
let tempRes = res;
while (keys.length > 1) {
const keyName = keys.shift();
tempRes = tempRes[keyName] || (tempRes[keyName] = {});
}
tempRes[keys[0]] = data[keyStr];
}
return res;
}
console.log(trans(translations));
function fn(obj = {}) {
const result = {}
Object.entries(obj).forEach(([key, value]) => {
const keys = key.split('.')
const lastIndex = keys.length - 1 // 减少重复计算
keys.reduce((acc, k, i) => {
if (i == lastIndex) {
acc[k] = value
} else if (acc[k] === undefined) {
acc[k] = {}
}
return acc[k]
}, result)
})
return result
}
我直接写的这个不知道为什么报错
const before = {
'editor.remove': 'Remove',
'editor.format': 'Format Painter',
'editor.draghandle.tooltip': 'Click And Drag',
'editor.copyToClipboard': 'CopyToClipboard',
'editor.importWrod.tooltip': 'Import Word',
'editor.slash': "Press '/' for commands",
'editor.slash.empty': 'No Result',
'editor.slash.format': 'Format',
'editor.slash.insert': 'Insert',
};
// 使用 lodash
const translations = {};
Object.keys(before).forEach((key) => {
const path = key.split('.');
loadsh.set(translations, path, before[key])
})
console.log(translations);
要将给定的扁平对象 translations
转换为嵌套对象,你可以通过编写一个递归函数来实现这一点。但是,由于你提供的原始 translations
对象已经是嵌套格式,看起来你可能是想询问如何从一个扁平格式转换到这种嵌套格式。不过,为了完整性,我将首先展示如何从扁平格式转换到嵌套格式,然后指出你提供的对象已经是嵌套格式。
假设你有一个扁平的对象,如下所示:
const flatTranslations = {
'editor.remove': 'Remove',
'editor.copy': 'Copy',
'editor.draghandle.tooltip': 'Click And Drag',
// ... 其他扁平键值对
};
你可以使用以下函数将其转换为嵌套对象:
function flattenToNested(flat, result = {}, path = []) {
for (let key in flat) {
let parts = key.split('.');
let current = result;
for (let i = 0; i < parts.length - 1; i++) {
let part = parts[i];
if (!current[part]) current[part] = {};
current = current[part];
}
current[parts[parts.length - 1]] = flat[key];
}
return result;
}
const nestedTranslations = flattenToNested(flatTranslations);
console.log(nestedTranslations);
然而,你提供的 translations
对象已经是一个嵌套对象,所以实际上你不需要进行任何转换。它已经是按照你想要的格式组织的。如果你想要访问某个特定的值,你可以直接通过键路径来访问它,例如:
console.log(translations.editor.remove); // 输出: "Remove"
console.log(translations.editor.draghandle.tooltip); // 输出: "Click And Drag"
因此,如果你正在寻找如何处理这个对象,你已经有了正确的格式,可以直接按照需要来访问和操作它。
// 最大值是1000 let num = 1000; num是最大数为1000,然后根据arr数组里面的test的值之和(3+4+5+6)为18, 1000 / 18 = 55(求整数); 然后需要得到的格式排列的数据如下: 麻烦各位大佬帮忙看看,写了循环,排列的数据格式不对,双循环的排列按顺序的话要怎么写?
let num = 600; num是最大数为600,然后根据arr数组里面的test的值之和(5+4+5+6)为20, 600 / 20 = 30; 希望得到下面的格式: 图片第一列5个输入框对应数组arr里面下标为0的test的值为5 图片第二列4个输入框对应数组arr里面下标为1的test的值为4 ... 控制1-30之间的随机数里面的输入框修改的时候只能输入1-30的数, 控制31-60之
js 数组的数据处理 这个2数字不是固定的, 如果a这个值是2 我需要得到arr为 arr = [9] 如果a这个值是4 我需要得到arr为 arr = [7,8,9] 大佬们帮我看看
arr数组最小长度是4最大是8 let num = 600;暂时写死600,这个数自定义输入的 麻烦各位大佬帮忙看看
在vue3中循环这个arr数组,怎么让数据竖向显示,如下图 这样双循环没有达到效果, 请问循环前是不是要处理一下arr这个数据
js:求一个数据处理方法,例如上述例子, tableList处理之后,可以得到 之前有如下方案 但是不能兼容account.t_sub_account_.*的场景,哪位大佬可以帮忙完善下吗?