3

Consider the following object:

const data = {
    foo: 'bar',
    items: [
        {
            id: 1,
            items: [
                {
                    id: 50,
                    content: 'test'
                }
            ]
        },
        {
            id: 2,
            items: [
                {
                    id: 70,
                    content: 'test'
                },
                {
                    id: 85,
                    content: 'test'
                }
            ]
        }
    ]
}

I am currently utilising the Vuex-i18n plugin which only supports string values within arrays, thus I need to iterate through my data and convert all arrays into objects.

I was hoping that I would be able to utilize JSON.parse in some way but I haven't been able to get it to work.

This is what I tried:

const json = '{"foo":"bar","items":[{"id":1,"items":[{"id":50,"content":"test"}]},{"id":2,"items":[{"id":70,"content":"test"},{"id":85,"content":"test"}]}]}';

console.log(JSON.parse(json, (key, value) =>
  typeof value === 'array'
    ? Object.assign({}, value)
    : value
));

Can anyone suggest a way in which this can be achieved? I was hoping that I could avoid recursively iterating over the object but I am not sure if that is possible...

Update

The expected output should look like this:

const data = {
    foo: 'bar',
    items: {
        0: {
            id: 1,
            items: {
                0: {
                    id: 50,
                    content: 'test'
                }
            }
        },
        1: {
            id: 2,
            items: {
                0: {
                    id: 70,
                    content: 'test'
                },
                1: {
                    id: 85,
                    content: 'test'
                }
            }
        }
    }
}

Note all the arrays are now objects...

9
  • Could you include an example of what the desired output is? Commented Oct 16, 2019 at 14:47
  • What is your expected output? Commented Oct 16, 2019 at 14:47
  • "convert all arrays into objects" what should this produce? An array-like? Something else? Commented Oct 16, 2019 at 14:48
  • Absolutely, give me 2 secs Commented Oct 16, 2019 at 14:48
  • 1
    no. you can not have nested objects without keys. Commented Oct 16, 2019 at 14:50

1 Answer 1

4

You could check for array with Array.isArray.

const json = '{"foo":"bar","items":[{"id":1,"items":[{"id":50,"content":"test"}]},{"id":2,"items":[{"id":70,"content":"test"},{"id":85,"content":"test"}]}]}';

console.log(JSON.parse(json, (key, value) => Array.isArray(value)
    ? Object.assign({}, value)
    : value
));
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

1 Comment

This seems to be exactly what I was looking for! Thank you very much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.