0

In my Vue project, I'm doing an API call to my Laravel (nova) backend, the data is returned as follows.

As you can see the data is an array, which contains an array of objects. Each array represents a record, while each object represents a field in that record.

There is the possibility that a record has unnecessary data.

const data = [
    [
        {
            attribute: 'id',
            value: 1,
            extra: "not needed data"
        },
        {
            attribute: 'title',
            value: 'The Title',
            extra: "not needed data"
        },
    ],
    [
        {
            attribute: 'id',
            value: 2
        },
        {
            attribute: 'title',
            value: 'The Other Title'
        },
    ],
]

I'm wondering how I can use Javascript to flatten this out so it becomes

const data = [
    {id: 1, title: 'The Title'},
    {id: 2, title: 'The Other Title'}
]

What I've tried

I've tried using a combination of map and filter, but the results don't even come close.

6
  • 1
    Your input's syntax is not valid. Your desired output's isn't either, actually Commented Aug 9, 2019 at 9:13
  • so is the output syntax, also post the code you have tried Commented Aug 9, 2019 at 9:14
  • Hmh I notice it's invalid but I don't see how.. What am I missing? An object can contain arrays, right? Commented Aug 9, 2019 at 9:17
  • "I've tried using a combination of map and filter..." - You've missed to post this part as well Commented Aug 9, 2019 at 9:17
  • Yes @Notflip, but it's required to have keys for its values. Just change the outer braces to square brackets [] and it's fine Commented Aug 9, 2019 at 9:18

1 Answer 1

1

You could use map with reduce, to build an object using only the attribute and value, ignoring all other properties:

const data = [
    [
        {
            attribute: 'id',
            value: 1,
            extra: "not needed data"
        },
        {
            attribute: 'title',
            value: 'The Title',
            extra: "not needed data"
        },
    ],
    [
        {
            attribute: 'id',
            value: 2
        },
        {
            attribute: 'title',
            value: 'The Other Title'
        },
    ],
]

console.log(data.map(a => a.reduce((a, o) => (a[o.attribute] = o.value, a), {})))

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

2 Comments

That's it Kobe! I always wonder how people can think of things like that, I feel like my brain can't understand reduce, or complex maps.. Thanks!
@Notflip No worries mate, I've only really just started to get good at using them. If you take time to fully understand how it works, it's a very nice tool to use for data manipulation. If you ever need more help, feel free to ask 👍

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.