This is my multi dimensional array.
Output should be in array2 structure.
I have tried many ways covert above array in easy but all came up with bulky code and lots of iteration.
Is there any easy way to do so?
I am a new in angular and JavaScript.
var array1=[
{
"id": 1,
"name": "Johny",
"category": [
{
"id": 12,
"name": "GUI",
"status": {
"id": 123,
"status": "working",
"name": "GUI"
}
},
{
"id": 13,
"name": "GFX",
"status": {
"id": 124,
"status": "working",
"name": "GFX"
}
},
{
"id": 14,
"name": "UI",
"status": {
"id": 125,
"status": "working",
"name": "UI"
}
}
]
},
{
"id": 2,
"name": "Paul",
"category": [
{
"id": 21,
"name": "GUI",
"status": {
"id": 212,
"status": "Progress",
"name": "GUI"
}
},
{
"id": 22,
"name": "GFX",
"status": {
"id": 221,
"status": "working",
"name": "GFX"
}
},
{
"id": 23,
"name": "DM",
"status": {
"id": 231,
"status": "done",
"name": "DM"
}
}
]
}
]
I want to change into this array2.
var array2=[
{
"id": 1,
"name": "Johny",
"category": [
{
"id": 12,
"name": "GUI",
"data": [
{
"id": 12,
"name": "GUI",
"status": {
"id": 123,
"status": "working",
"name": "GUI"
}
},
{
"id": 21,
"name": "GUI",
"status": {
"id": 212,
"status": "Progress",
"name": "GUI"
}
}
]
},
{
"id": 13,
"name": "GFX",
"data": [
{
"id": 13,
"name": "GFX",
"status": {
"id": 124,
"status": "working",
"name": "GFX"
}
},
{
"id": 22,
"name": "GFX",
"status": {
"id": 221,
"status": "working",
"name": "GFX"
}
}
]
},
{
"id": 14,
"name": "UI",
"data": [
{
"id": 14,
"name": "UI",
"status": {
"id": 125,
"status": "working",
"name": "UI"
}
},
//null data if not in first
{}
]
},
{
"id": 23,
"name": "DM",
"data": [
//null data if not in first
{},
{
"id": 23,
"name": "DM",
"status": {
"id": 231,
"status": "done",
"name": "DM"
}
}
]
}
]
},
{
"id": 2,
"name": "Paul",
"category": [
{
"id": 21,
"name": "GUI",
"data": [
{
"id": 12,
"name": "GUI",
"status": {
"id": 123,
"status": "working",
"name": "GUI"
}
},
{
"id": 21,
"name": "GUI",
"status": {
"id": 212,
"status": "Progress",
"name": "GUI"
}
}
]
},
{
"id": 22,
"name": "GFX",
"data": [
{
"id": 13,
"name": "GFX",
"status": {
"id": 124,
"status": "working",
"name": "GFX"
}
},
{
"id": 22,
"name": "GFX",
"status": {
"id": 221,
"status": "working",
"name": "GFX"
}
}
]
},
{
"id": 14,
"name": "UI",
"data": [
{
"id": 14,
"name": "UI",
"status": {
"id": 125,
"status": "working",
"name": "UI"
}
},
//null data if not in first
{}
]
},
{
"id": 23,
"name": "DM",
"data": [
//null data if not in first
{},
{
"id": 23,
"name": "DM",
"status": {
"id": 231,
"status": "done",
"name": "DM"
}
}
]
}
]
}
]
Output should be in array2 structure.
Thanks in advance.
So you just want to have the original data as additional data attribute? This is simple using a forEach() loop in combination with the map() function:
array1.forEach((obj) => {
obj.category = obj.category.map((cat) => {
return {id: cat.id, name: cat.name, data: cat}
});
})
Hope this is what you're looking for :-)
Related
I am calling an API from a server, and I am getting this as a response. A part of the data I am getting -
{
"id": 322854,
"date": "2022-09-20T18:00:00+00:00",
"time": "18:00",
"timestamp": 1663696800,
"timezone": "UTC",
"stage": null,
"week": null,
"status": {
"long": "Game Finished",
"short": "FT",
"timer": null
},
"league": {
"id": 9,
"name": "French Cup",
"type": "cup",
"season": 2022,
"logo": "https://media-2.api-sports.io/basketball/leagues/9.png"
},
"country": {
"id": 4,
"name": "France",
"code": "FR",
"flag": "https://media-2.api-sports.io/flags/fr.svg"
},
"teams": {
"home": {
"id": 22,
"name": "Boulazac",
"logo": "https://media-2.api-sports.io/basketball/teams/22.png"
},
"away": {
"id": 2294,
"name": "CEP Lorient",
"logo": "https://media-2.api-sports.io/basketball/teams/2294.png"
}
},
"scores": {
"home": {
"quarter_1": 16,
"quarter_2": 12,
"quarter_3": 21,
"quarter_4": 26,
"over_time": null,
"total": 75
},
"away": {
"quarter_1": 9,
"quarter_2": 16,
"quarter_3": 19,
"quarter_4": 23,
"over_time": null,
"total": 67
}
}
}
{
"id": 322854,
"date": "2022-09-20T18:00:00+00:00",
"time": "18:00",
"timestamp": 1663696800,
"timezone": "UTC",
"stage": null,
"week": null,
"status": {
"long": "Game Not Started",
"short": "NS",
"timer": null
},
"league": {
"id": 9,
"name": "French Cup",
"type": "cup",
"season": 2022,
"logo": "https://media-2.api-sports.io/basketball/leagues/9.png"
},
"country": {
"id": 4,
"name": "France",
"code": "FR",
"flag": "https://media-2.api-sports.io/flags/fr.svg"
},
"teams": {
"home": {
"id": 22,
"name": "Boulazac",
"logo": "https://media-2.api-sports.io/basketball/teams/22.png"
},
"away": {
"id": 2294,
"name": "CEP Lorient",
"logo": "https://media-2.api-sports.io/basketball/teams/2294.png"
}
},
"scores": {
"home": {
"quarter_1": 16,
"quarter_2": 12,
"quarter_3": 21,
"quarter_4": 26,
"over_time": null,
"total": 75
},
"away": {
"quarter_1": 9,
"quarter_2": 16,
"quarter_3": 19,
"quarter_4": 23,
"over_time": null,
"total": 67
}
}
}
There are a bunch of such objects in an array each having a different fixture. As you can see that status?.short === "NS" in one while the other is status?.short === "FT". Now, I want to filter the data using these properties.
<div className="fixturesTab-container">
<div className="fixturesTab-container__header">
<h4>Fixtures</h4>
</div>
<div className="fixturesTab-container__body">
{fixtures.map((fixture) => (
<div
key={fixture?.id}
className="fixturesTab-container__body__box"
>
......
</div>
))}
</div>
</div>
I want the data to be displayed in such a manner that when the user browse to that page and the API is being fetched, eventhough the entire data (fixtures) is being displayed in the page. I want the fixtures that have not yet started, i.e has the property of status?.short === "NS", to be displayed in the window that is visible to the user. And the rest data can be acquired by either scrolling up, which will have the fixtures that have been completed, or by scrolling down which will have the fixtures that have not yet been started.
If, I am unable to understand what I want properly, then you can check out this page . This is exactly what I am planning to achieve.
I have an array called $customerRecords. I want to group the data in this array by the customer's email.
This is the array below
$customerRecords = [
{
"id": 1,
"note": "This is note 1",
"customer": [
{
"id": 1,
"user_id": 34,
"email": "doe#mailnator.com",
"phone": "9829484857"
}
]
},
{
"id": 2,
"note": "This is note 2",
"customer": [
{
"id": 2,
"user_id": 34,
"email": "john#mailnator.com",
"phone": "9829484857"
}
]
},
{
"id": 3,
"note": "This is a note 3",
"customer": [
{
"id": 2,
"user_id": 34,
"email": "john#mailnator.com",
"phone": "9829484857"
}
]
},
]
This is the expected result I want to achieve so that I can know the group of data that belongs to an email .
{
"doe#mailnator.com": [
{
"id": 1,
"note": "This is note 1",
"customer": [
{
"id": 1,
"user_id": 34,
"email": "doe#mailnator.com",
"phone": "9829484857"
}
]
}
],
"john#mailnator.com": [
{
"id": 2,
"note": "This is note 2",
"customer": [
{
"id": 2,
"user_id": 34,
"email": "john#mailnator.com",
"phone": "9829484857"
}
]
},
{
"id": 3,
"note": "This is a note 3",
"customer": [
{
"id": 2,
"user_id": 34,
"email": "john#mailnator.com",
"phone": "9829484857"
}
]
}
]
}
So this is what I have tried but it's not working:
return collect($customerRecords)->groupBy('customer.email')
you are almost done just define customer 0 item then email
return collect($customerRecords)->groupBy('customer.0.email');
This is how I was able to solve it.
$grouped = [];
foreach($customerRecords as $value) {
foreach($value['customer'] as $cust) {
$grouped[$cust['email']][] = $value;
}
}
companyList = data['fields'][0]['choices'];
if (companyList.length != 0) {
int val;
for (val = 0; val < companyList.length; val++) {
final controller = TextEditingController();
final field = xTextfield(
txtlabel: "Values",
iconfield: bookmarksIcon,
);
setState(() {
_controllers.add(controller);
_fields.add(field);
});
}
}
But this works only if json response is same when it changes and Array object is differnet i got error here is my json that will be different everytime e.g
"inputtype": "dropdown", will make a dropdown widget and all data will be put into it and so on
{ "fields": [ { "id": 31, "name": "make", "isrequired": "required", "valuetype": "text", "priority": 1, "inputtype": "dropdown", "max_min": [], "rangeable": "false", "choices": [ { "id": 46, "name": "Samsung", "categoryextrafield_id": 31, "created_at": "2021-12-29T01:30:47.000000Z", "updated_at": "2021-12-29T01:30:47.000000Z", "priority": 10 }, { "id": 47, "name": "Dell", "categoryextrafield_id": 31, "created_at": "2021-12-29T01:30:52.000000Z", "updated_at": "2021-12-29T01:30:52.000000Z", "priority": 20 }, { "id": 48, "name": "IBM", "categoryextrafield_id": 31, "created_at": "2021-12-29T01:31:09.000000Z", "updated_at": "2021-12-29T01:31:09.000000Z", "priority": 30 }, { "id": 49, "name": "Acer", "categoryextrafield_id": 31, "created_at": "2021-12-29T01:31:24.000000Z", "updated_at": "2021-12-29T01:31:24.000000Z", "priority": 40 } ], "available": [] }, { "id": 32, "name": "model", "isrequired": "required", "valuetype": "text", "priority": 2, "inputtype": "textfield", "max_min": [], "rangeable": "false", "choices": [], "available": [ { "model": "a51" }, { "model": "y9s" }, { "model": "a31" }, { "model": "yS10" }, { "model": "Y10S" }, { "model": "A551" }, { "model": "node8" }, { "model": "s9" }, { "model": null }, { "model": "2021" }, { "model": "2020" }, { "model": "2010" }, { "model": "Civic" }, { "model": "2019" }, { "model": "Daewooy9" }, { "model": "corei5" }, { "model": "corei9" }, { "model": "corei3" }, { "model": "corei11" } ] }, { "id": 29, "name": "features", "isrequired": "required", "valuetype": "text", "priority": 3, "inputtype": "checkbox", "max_min": [], "rangeable": "false", "choices": [ { "id": 41, "name": "Bluetooth", "categoryextrafield_id": 29, "created_at": "2021-12-29T01:19:00.000000Z", "updated_at": "2021-12-29T01:19:00.000000Z", "priority": 1 }, { "id": 42, "name": "Fingerprint", "categoryextrafield_id": 29, "created_at": "2021-12-29T01:19:10.000000Z", "updated_at": "2021-12-29T01:19:10.000000Z", "priority": 10 }, { "id": 43, "name": "LedDisplay", "categoryextrafield_id": 29, "created_at": "2021-12-29T01:19:35.000000Z", "updated_at": "2021-12-29T01:19:35.000000Z", "priority": 15 } ], "available": [] }, { "id": 30, "name": "condition", "isrequired": "required", "valuetype": "text", "priority": 4, "inputtype": "radiobutton", "max_min": [], "rangeable": "false", "choices": [ { "id": 44, "name": "Used", "categoryextrafield_id": 30, "created_at": "2021-12-29T01:20:31.000000Z", "updated_at": "2021-12-29T01:20:31.000000Z", "priority": 10 }, { "id": 45, "name": "New", "categoryextrafield_id": 30, "created_at": "2021-12-29T01:20:38.000000Z", "updated_at": "2021-12-29T01:20:38.000000Z", "priority": 20 } ], "available": [] } ] }
You can use the Dynamic Widget package.
Just pass the widget as JSON data from the server and use a FutureBuilder to build it when your data arrives.
You will also need to change your JSON data accordingly.
I have the following JSON object from which I'd like to extract a specific property
{
"gender": "male",
"age": 36,
"profession": "teacher",
"hobbies": [
{
"id": 28,
"name": "gardening"
},
{
"id": 878,
"name": "Football"
},
{
"id": 35,
"name": "Reading"
},
{
"id": 10751,
"name": "Socialising"
}
],
"Country": "Sweden",
"id": 4542236,
}
Lets say I wanted to create a new object that hold only the "hobbies" property with all the values like this
{
"hobbies": [
{
"id": 28,
"name": "gardening"
},
{
"id": 878,
"name": "Football"
},
{
"id": 35,
"name": "Reading"
},
{
"id": 10751,
"name": "Socialising"
}
]
}
Since the JSON is coming from an external API I have no control on future positioning (index) of any properties
I am trying to make a shopping cart API route and I am stuck with this problem because when I am using this statement $carts = \Cart::session($session)->getContent();
I get an object like this:
"cart": {
"1": {
"id": 1,
"name": "Tort cu biscuiți",
"price": 20,
"quantity": "1",
"attributes": {
"image_path": "http://127.0.0.1:8000/storage/images/AkiGKJdxjKNtyoVI034RPL1drLsMntUxLfzqZplV.jpeg"
},
"conditions": []
},
"2": {
"id": 2,
"name": "Tort cu biscuiți",
"price": 20,
"quantity": 2,
"attributes": {
"image_path": "http://127.0.0.1:8000/storage/images/IkAhenLttHWaRD58hNZ460ykWCq7q1sih3vI9H5V.jpeg"
},
"conditions": []
}
}
and I want to convet this to be array of objects. I tryied $cart->toArray(); and didn't work, also I tiyed (array) $cart and I get this:
"cart": {
"\u0000*\u0000items": {
"1": {
"id": 1,
"name": "Tort cu biscuiți",
"price": 20,
"quantity": "1",
"attributes": {
"image_path": "http://127.0.0.1:8000/storage/images/AkiGKJdxjKNtyoVI034RPL1drLsMntUxLfzqZplV.jpeg"
},
"conditions": []
},
"2": {
"id": 2,
"name": "Tort cu biscuiți",
"price": 20,
"quantity": 2,
"attributes": {
"image_path": "http://127.0.0.1:8000/storage/images/IkAhenLttHWaRD58hNZ460ykWCq7q1sih3vI9H5V.jpeg"
},
"conditions": []
}
}
}
may I know what is the problem?
You can do it manuel like this:
$carts = \Cart::session($session)->getContent();
if ($carts) {
$cartsArray = json_decode($carts);
foreach($cartsArray->cart as &$cart){
$cart = (array) $cart;
}
}
dd($cartsArray);