1

I'm having difficulties to remove an additional set of square brackets or avoid including them within array.

My JavaScript code currently accesses array's object at 0 index and therefore I'd have to loop in one level deeper array. Which I'd like to avoid if possible.

This is my endpoint:

'pricelist/<entryId:\d+>.json' => function($entryId) {
    return [
        'elementType' => ElementType::Entry,
        'criteria' => [
            'section' => "products",
            'descendantOf' => $entryId,
        ],
        'transformer' => function(EntryModel $entry) {

            $products = [];
            // "orderItem" is entry field
            foreach ( $entry->orderItem as $product ) {
                $products[] = [
                    'title' => $product->titleVariation,
                ];
            }
            return $products;
        },
    ];
},

Returned JSON array is:

{"data":
    [
        [
            {
                "title": "a",
            },
            {
                "title": "b",
            },
            {
                "title": "c",
            }
        ]
    ]
}

Expected JSON array output:

{"data":
    [
        {
            "title": "a",
        },
        {
            "title": "b",
        },
        {
            "title": "c",
        }
    ]
}

I have almost the same scenario from this question but the answer wasn't my issue.

  • Did you checked 'title' => $product->titleVariation while adding to array.I mean is there any need to wrap title in [ ] – inrsaurabh Jun 27 '19 at 9:33
  • @inrsaurabh not wrapping it gives a syntax error because the key must be within []. And furthermore not assigning key name gives the same output - two dimensional array. – Dominik Krulak Jun 27 '19 at 10:26
1

change $products[] = to $products = in your for each loop, that should fix the extra square brackets. And if you want to get all entries and their titleVariation fields you should use 'elementType' => Entry::Class

'pricelist/<entryId:\d+>.json' => function($entryId) {
    return [
        'elementType' => Entry::Class,
        'criteria' => [
            'section' => 'products',
            'descendantOf' => $entryId,
        ],
        'transformer' => function(Entry $entry) {

            $products = [];
            // "orderItem" is entry field
            foreach ( $entry->orderItem as $product ) {
                $products = [
                    'title' => $product->titleVariation,
                ];
            }
            return $products;
        },
    ];
},
|improve this answer|||||
  • Thanks for the respond @HendrikEng. It will always overwrite the key so last item in array will return. It means that if entry field has more entries only last one will be returned. – Dominik Krulak Jun 27 '19 at 13:42
  • do you want to get all title Variation fields of each entry in the section products? – HendrikEng Jun 27 '19 at 13:51
  • updated the answer, it will now get all entries in products and loop over all orderItems and the titleVariation fields – HendrikEng Jun 27 '19 at 13:59
  • First I access descendants of entryId. Than in descendant entry I access entry field orderItem. Than I must loop for in fields that I want to access. Field titleVariation is just an example so the code is not complicated. – Dominik Krulak Jun 27 '19 at 14:00
  • 1
    now i finally got what you mean , yes you will need it since the data array holds each entry which then holds an array of the titleVariation objects. – HendrikEng Jun 27 '19 at 14:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.