3

I have an array that I successfully transform into a string format with an EcmaScript-6 function. However, how can I do this using EcmaScript-3? The receiving endpoint requires an ES3 function.

Original array:

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"}
]

Required string format

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

My working solution with ES6:

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

My take on converting to ES3:

var res = formdata.map(({name,value,focus,type}) { 
  ("name" + "." + "type" + "|" + "value" + "focus").join(;)
}

This solution is obviously not working and also I am not sure whether it is valid ES3 JavaScript.

Thanks!

3
  • 7
    Use Babel.js to convert ES6 to older versions. Commented Apr 4, 2019 at 5:10
  • map is added in ES5, so that will obviously create a problem. You can use a simple for loop to iterate over the array, and create the string. Commented Apr 4, 2019 at 5:11
  • 1
    You original array is not valid JavaScript. Nor is the "required string format" and ES3 version. Commented Apr 4, 2019 at 5:14

1 Answer 1

5

Issues with your ES3 attempt:

  • The arrow function is missing the arrow, but is also an ES6 addition to the syntax
  • .join(;) is not valid JavaScript. .join(";") is.
  • destructuring syntax was introduced in ES6
  • .map was introduced in ES5
  • You need one more "|"

So:

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

var arr = [];
for (var i = 0; i < formdata.length; i++) {
    var elem = formdata[i];
    arr.push(elem.name + "." + elem.type + "|" + elem.value + "|" + elem.focus);
}
var str = arr.join(";");

console.log(str);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.