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 option to make difference between object & array #115

Open
tangkhaiphuong opened this issue Oct 29, 2020 · 1 comment
Open

What option to make difference between object & array #115

tangkhaiphuong opened this issue Oct 29, 2020 · 1 comment

Comments

@tangkhaiphuong
Copy link

tangkhaiphuong commented Oct 29, 2020

I make a sample to test the

const flat = require("flat");

const data = {
  object: {
    '0': 2,
  },
  array: [2],
};

console.log('Before', data);

const result = flat.flatten(data);

console.log("Flatten", result);

const atad = flat.unflatten(result);

console.log('After', atad);

And the result

Before { object: { '0': 2 }, array: [ 2 ] }
Flatten { 'object.0': 2, 'array.0': 2 }    
After { object: [ 2 ], array: [ 2 ] }  

The object is flatted and unflatten is not the same as the original. It should be the same. I think the problem from

Flatten { 'object.0': 2, 'array.0': 2 }

It should be

Flatten { 'object.0': 2, 'array[0]': 2 }

This behavior should similar to qs library. So please provide the option to do that.

@MattJLeach
Copy link

MattJLeach commented Dec 8, 2020

@tangkhaiphuong

I've got the same issue so I've played around with the flatten function to get it working. I've taken out the opts for now to keep it simple (and done it in typescript albeit with some any's). If I get a chance to put the opts back in I'll do a PR. The main issue is with the delimiter option as you now have 2 ('.' for object and brackets for an array).

Here's what I've got:

function flatten(object: any): { [key: string]: any } {
    const output: any = {};

    function step(object: any, prev?: any, currentDepth?: number, useBrackets?: boolean) {
        const depth = currentDepth || 1;
        Object.keys(object).forEach(function (key) {
            const value = object[key];
            const isarray = Array.isArray(value);
            const type = Object.prototype.toString.call(value);
            const isobject = type === '[object Object]' || type === '[object Array]';

            const newKey = prev
                ? `${prev}${useBrackets ? '[' : '.'}${key}${useBrackets ? ']' : ''}`
                : key;

            if (isobject && Object.keys(value).length) {
                return step(value, newKey, depth + 1, isarray);
            }

            output[newKey] = value;
        });
    }

    step(object);

    return output;
}

I'm looking at the unflatten function now as this doesn't work with arrays and objects in this way

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