Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pushing data in keys into a single array only values

Such data is coming from Laravel to Ajax. How do I push the values ​​inside the keys into a single array without the keys.

$get_session = session()->get('cart.products');
return response($get_session);

Data returned to Ajax.

data1: {
1234543: {id: 1, title: 'Product Title1', description: 'Product Descrition1'}
3453234: {id: 2, title: 'Product Title2', description: 'Product Descrition2'}
4564234: {id: 3, title: 'Product Title3', description: 'Product Descrition3'} 
}

data2: {
4643345: {id: 4, title: 'Product Title4', description: 'Product Descrition4'}
8679673: {id: 5, title: 'Product Title5', description: 'Product Descrition5'}
2344565: {id: 6, title: 'Product Title6', description: 'Product Descrition6'}
}

this is what i want to do

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[
1234543: {id: 1, title: 'Product Title1', description: 'Product Descrition1'}
3453234: {id: 2, title: 'Product Title2', description: 'Product Descrition2'}
4564234: {id: 3, title: 'Product Title3', description: 'Product Descrition3'} 
4643345: {id: 4, title: 'Product Title4', description: 'Product Descrition4'}
8679673: {id: 5, title: 'Product Title5', description: 'Product Descrition5'}
2344565: {id: 6, title: 'Product Title6', description: 'Product Descrition6'}
]

How can I do it with Laravel or jquery? Thanks everyone.

>Solution :

Say you get the ajax response as response, you can convert it to the expected object like so

var data = Object.assign(...Object.values(response));

Example

var response = {
    data1: {
        1234543: {id: 1, title: 'Product Title1', description: 'Product Descrition1'},
        3453234: {id: 2, title: 'Product Title2', description: 'Product Descrition2'},
        4564234: {id: 3, title: 'Product Title3', description: 'Product Descrition3'}
    },
    data2: {
        4643345: {id: 4, title: 'Product Title4', description: 'Product Descrition4'},
        8679673: {id: 5, title: 'Product Title5', description: 'Product Descrition5'},
        2344565: {id: 6, title: 'Product Title6', description: 'Product Descrition6'}
    }
};
var data = Object.assign(...Object.values(response));
console.log(data)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading