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

时间:2019-04-03 16:13:26

标签: javascript arrays stringify

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

这是数组:

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,"");

他们都没有工作。

任何想法如何解决这个问题?

谢谢!

3 个答案:

答案 0 :(得分:1)

我认为只需在数组上使用map()。并返回模板字符串。然后join()的{​​{1}}数组

';'

答案 1 :(得分:1)

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

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);

答案 2 :(得分:0)

不清楚您要问的是什么,因为您的代码段不是有效的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}`);