Wrong formatting in arrays, fix.
See original GitHub issueHi, @therealparmesh I’m using object-to-formdata is very usefully.
Well I have the following example data to convert:
{
"phones": [
{
"number": 9999999,
"code": 52,
"contact": "Maria"
}
]
};
I convert the information and process it into php and the result of array is the follow:
[
'phones' => [
[
'number' => '9991944318',
],
[
'code' => '52',
],
[
'contact' => 'Maria',
],
],
]
as you can see each item is within an array.
I solved this problem as follows:
function objectToFormData (obj, fd, pre) {
fd = fd || new FormData()
Object.keys(obj).forEach(function (prop) {
var key = pre ? (pre + '[' + prop + ']') : prop
if (isObject(obj[prop]) && !isArray(obj[prop]) && !isFile(obj[prop])) {
objectToFormData(obj[prop], fd, key)
} else if (isArray(obj[prop])) {
// HERE.
// I get the index from the array
obj[prop].forEach(function (value, index) {
// HERE.
// I pass the index to makeArrayKey.
var arrayKey = makeArrayKey(key, index)
if (isObject(value) && !isFile(value)) {
objectToFormData(value, fd, arrayKey)
} else {
fd.append(arrayKey, value)
}
})
} else {
fd.append(key, obj[prop])
}
})
// HERE.
// I receive the index as default "null".
function makeArrayKey (key, index = null) {
if (key.length > 2 && key.lastIndexOf('[]') === key.length - 2) {
console.log('aqui en solo key');
return key
} else {
// HERE.
// I valid the existence of the index to indicate that it
// is added within the same array.
return index != null ? key + '['+index+']' : key + '[]';
}
}
And then the result is the follow It’s just like I expected.
[
'phones' => [
[
'number' => '9991944318',
'code' => '52',
'contact' => 'Maria',
],
],
]
That’s all. Thank you.
Issue Analytics
- State:
- Created 6 years ago
- Comments:8 (5 by maintainers)
Top Results From Across the Web
An array with objects is formatted incorrectly. #40989 - GitHub
An array with objects is formatted incorrectly. This is similar to #50284. VSCode Version: 1.50.0-insider; OS Version: macOS Catalina 10.15.
Read more >How To Fix Excel Error You Cannot Change Part Of An Array?
In the array formulas, you can use the Excel built-in functions. So, make a press over the Ctrl+Shift+Enter for entering the formula. Excel...
Read more >Fixing Incorrect Python Array Format - Stack Overflow
For some reason I cannot print either the entire array or loop through in the correct format. Each entry in the array should...
Read more >Rules for changing array formulas - Microsoft Support
If you've entered a single-cell array formula, select the cell, press F2, make your changes, and then press Ctrl+Shift+Enter.. If you've entered a...
Read more >49678 – Code formatting of arrays does not follow ... - Bugs
When you enter such a bug report, try to attach your preference profile. Go to the code formatter preference page, select your profile...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Closing this as I added a configuration flag to support this use case in a recent commit.
Will give this a test over the weekend and get back to you 😃