Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What does it should be when we call toArray()? #107

Closed
RaulWei opened this issue Jun 21, 2017 · 6 comments
Closed

What does it should be when we call toArray()? #107

RaulWei opened this issue Jun 21, 2017 · 6 comments

Comments

@RaulWei
Copy link

RaulWei commented Jun 21, 2017

Suppose there is an object collection like that

people = {
  name: 'wei',
  sex: 'male',
  phone: [15211111111, 13511111111],
  company: {
    name: 'IBM',
    city: 'BJ'
  }
}

What does it should be if we call collect(people).toArray()?
In current implementation, it will return
[ 'wei', 'male', [ 15211111111, 13511111111 ], { name: 'IBM', city: 'BJ' } ].
Should it be [ 'wei', 'male', [ 15211111111, 13511111111 ], [ 'IBM', 'BJ' ] ]?

@g-plane
Copy link
Contributor

g-plane commented Jun 21, 2017

I don't think [ 'wei', 'male', [ 15211111111, 13511111111 ], [ 'IBM', 'BJ' ] ] is consistent with Laravel.
For example (in Laravel 5.4):

$collect = collect([
    'name' => 'wei',
    'sex' => 'male',
    'phone' => [15211111111, 13511111111],
    'company' => ['name' => 'IBM', 'city' => 'BJ']
]);

return dd($collect->toArray());

The result is:
image

@ecrmnn
Copy link
Owner

ecrmnn commented Jun 21, 2017

It's not consistent with Laravel right now because in PHP have associative arrays, while JS don't.

I think it would be weird to return an object from a method called toArray(), but it would be consistent with Laravel.

What do you think? I'm open for a discussion

@solomonhawk
Copy link

solomonhawk commented Jun 21, 2017

It is my (random JS developer) opinion that the closest equivalent data structure in JS would be a nested array of [key, value] pairs. This is admittedly more awkward to work with than an object or real associative array. I wonder what the use case is for calling toArray() on an object wrapped in collect.

Input:

people = {
  name: 'wei',
  sex: 'male',
  phone: [15211111111, 13511111111],
  company: {
    name: 'IBM',
    city: 'BJ'
  }
}

Output:

[
  ['name', 'wei'],
  ['sex', 'male'],
  ['phone', [15211111111, 13511111111]],
  ['company', { name: 'IBM', city: 'BJ' }]
]

@wmj0cn
Copy link

wmj0cn commented Jun 25, 2017

dont see the usefulness of this method, would you like to give a real use scenario of it?

@faalotaiby
Copy link

In my opinion calling it ToArray is not useful, unless you needed an array, that says pass it to method which only accepted Integer() as a parameter.

@RaulWei
Copy link
Author

RaulWei commented Jun 26, 2017

Sorry for that I'm not familiar with Laravel, I propose this problem standing at the general position.

@ecrmnn ecrmnn closed this as completed Jun 28, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants