N
N
nemovalex2021-10-10 17:38:16
JavaScript
nemovalex, 2021-10-10 17:38:16

How to get data (array) without disturbing the order of Yii2/Axios elements?

At my backend, an array is given in the order in which I need it.

$list = Category::getSortedList();

\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;

return (isset($list) && !empty($list)) ? $list : [];

And I get it via Axios:
try {
        const response = await axios.get(url);
        if(response.data !== null && response.data !== undefined){
           return Object.values(response.data);
        }else {
            return [];
        }

    }catch(error){
        console.log(error);
    }

And for some reason, the data array itself is sorted and the order that was needed is violated. PS At first I sinned on Object.values(), but I immediately output response.data to the console and the order of the elements is already violated there. How can I get this data without breaking it.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
nemovalex, 2021-10-11
@nemovalex

Decision:

$list = Category::getSortedList();
return (isset($list) && !empty($list)) ? json_encode(array_values($list)) : [];

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question