Home > Enterprise >  Pushing data in keys into a single array only values
Pushing data in keys into a single array only values

Time:02-11

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

[
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.

CodePudding user response:

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)

CodePudding user response:

try this

return response()->json([$get_session['data1']]);
  • Related