question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Wrong formatting in arrays, fix.

See original GitHub issue

Hi, @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:closed
  • Created 6 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
therealparmeshcommented, Mar 26, 2018

Closing this as I added a configuration flag to support this use case in a recent commit.

0reactions
benojcommented, Feb 24, 2018

Will give this a test over the weekend and get back to you 😃

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found