将多维数组转换为特定的字符串格式

Convert multidimensional Array to specific String format

我收到一个数组,我必须将其转换为特定的字符串格式。

这是数组:

formdata: [
    1: {name: "gender", value: "F", focus: 0, type: "radio"}
    2: {name: "firstname", value: "empty", focus: 0, type: "input"}
    3: {name: "lastname", value: "empty", focus: 0, type: "input"}
    4: {name: "birthday", value: "empty", focus: 0, type: "input"}
    5: {name: "street", value: "empty", focus: 0, type: "input"}
    6: {name: "streetNo", value: "empty", focus: 0, type: "input"}
]

这是应转换为的特定字符串格式:

let formdata = gender.radio|F|0;firstName.text|empty|1;lastName.text|empty|0;street.text|empty|0;houseNumber.text|empty|0;zip.text|empty|0;city.text|empty|0;country.select-one|de|0;birthdate-day.text|empty|0;birthdate-month.text|empty|0;birthdate-year.text|empty|0;email.email|empty|0;code.text|filled_out|0

我认为最好使用 JSON.stringify。我已经尝试了几种变体:

let formdata = JSON.stringify(
formdata: [
    1: {name: "gender", value: "F", focus: 0, type: "radio"}
    2: {name: "firstname", value: "empty", focus: 0, type: "text"}
    3: {name: "lastname", value: "empty", focus: 0, type: "text"}
    4: {name: "birthday", value: "empty", focus: 0, type: "text"}
    5: {name: "street", value: "empty", focus: 0, type: "text"}
    6: {name: "streetNo", value: "empty", focus: 0, type: "text"}
])
.replace(/(\]\]\,)\[/g, "]\n")..replace(/(\[\[|\]\]|\")/g,"");

None 他们成功了。

有什么解决办法吗?

谢谢!

我想只需在数组上使用 map() 即可。和 return 模板字符串。然后 join() 数组由 ';'

const formdata = [
    {name: "gender", value: "F", focus: 0, type: "radio"},
    {name: "firstname", value: "empty", focus: 0, type: "input"},
    {name: "lastname", value: "empty", focus: 0, type: "input"},
    {name: "birthday", value: "empty", focus: 0, type: "input"},
    {name: "street", value: "empty", focus: 0, type: "input"},
    {name: "streetNo", value: "empty", focus: 0, type: "input"},
]


let res = formdata.map(({name,value,focus,type}) => `${name}.${type}|${value}|${focus}`).join(';')

console.log(res)

不清楚你在问什么,因为你的代码片段无效 javascript。

但是给定数组

  let formData = [
    {name: "gender", value: "F", focus: 0, type: "radio"},
    {name: "firstname", value: "empty", focus: 0, type: "input"},
    {name: "lastname", value: "empty", focus: 0, type: "input"},
    {name: "birthday", value: "empty", focus: 0, type: "input"},
    {name: "street", value: "empty", focus: 0, type: "input"},
    {name: "streetNo", value: "empty", focus: 0, type: "input"},
  ];

如果你想要一个字符串

'gender.radio|F|0;firstName.text|empty|1;lastName.text|empty|0;street.text|empty|0;houseNumber.text|empty|0;zip.text|empty|0;city.text|empty|0;country.select-one|de|0;birthdate-day.text|empty|0;birthdate-month.text|empty|0;birthdate-year.text|empty|0;email.email|empty|0;code.text|filled_out|0'

那么这将起作用:

let formDataString = formData.map(({name, value, focus, type}) =>
    `${name}.${type}|${value}|${focus}`);

您可以使用一个辅助字符串作为分隔符,一个对象作为替换值,一个数组作为所需的键。

var array = [{ name: "gender", value: "F", focus: 0, type: "radio" }, { name: "firstname", value: "empty", focus: 0, type: "input" }, { name: "lastname", value: "empty", focus: 0, type: "input" }, { name: "birthday", value: "empty", focus: 0, type: "input" }, { name: "street", value: "empty", focus: 0, type: "input" }, { name: "streetNo", value: "empty", focus: 0, type: "input" }],
    keys = ['name', 'type', 'value', 'focus'],
    take = { type: { input: 'text' } },
    separators= '.||',
    string = array
        .map(o => keys
            .map((k, i) => (take[k] && take[k][o[k]] || o[k]) + (separators[i] || ''))
            .join('')
        )
        .join(';');

console.log(string);