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

Numeric-key objects result in sparse arrays #66

Open
dharkness opened this issue Apr 30, 2021 · 2 comments
Open

Numeric-key objects result in sparse arrays #66

dharkness opened this issue Apr 30, 2021 · 2 comments

Comments

@dharkness
Copy link

dharkness commented Apr 30, 2021

Reading through some of the other issues, it seems this may be intended behavior. We have enabled the option to output brackets around array indices.

{
  items: [
    {
      123: 'foo',
      456: 'bar'
    }
  ]
}

// dot.dot(obj)

{
  'items[0].123': 'foo',
  'items[0].456': 'bar'
}

But when you run that through dot.object(), you don't get the original object. You get a sparse array instead.

{
  items: [
    [
      ...123 empty slots...
      'foo',
      ...333 empty slots...
      'bar'
    ]
  ]
}

Is there an option to use objects for . in all cases, even when the key is an integer?

@adamperez
Copy link

Running into this issue as well. Would love some resolution.

@adamperez
Copy link

@dharkness unsure if you're still dealing with this problem, but I seem to have found a workaround. Fortunately or unfortunately, you will need to modify your code a bit to init a Dot class, with the useArray flag set to false:

const util = new dot('.', false, false, true);

Once that's done, you can run the object method on your object and you should get the results you're expecting:

let dot = require('dot-object');
let util = new dot('.', false, false, true);
let data = {
  items: [
    {
      123: 'foo',
      456: 'bar'
    }
  ]
};

let results = util.object(data);

where results is { items: [ { '123': 'foo', '456': 'bar' } ] }

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

2 participants