Home > Net >  Dart: How to create new list Using Existing List?
Dart: How to create new list Using Existing List?

Time:02-28

i am new to flutter . I am trying to create new list from below list

 var itemlist1=[
          {"p_id": "a101", "model": "M-Plaz","price": 2500},
          {"p_id": "a101", "model": "Z-Plaz","price": 3500},
          {"p_id": "a102", "model": "M-Neo", "price": 1560},
          {"p_id": "a102", "model": "N-Neo1","price": 3600}];

Output list should be like below

var newlist=[{"Subitems":[
    {
        "p_id":"a101",
        "items": [
            {"p_id": "a101", "model": "M-Plaz","price": 2500},
          {"p_id": "a101", "model": "Z-Plaz","price": 3500}     
        ]},{
        "p_id": "a102",
        "items": [
           {"p_id": "a102", "model": "M-Neo", "price": 1560},
          {"p_id": "a102", "model": "N-Neo1","price": 3600}
        ]},
    ]
}];

please i need help..

CodePudding user response:

You can do this by:

newlist.addAll([
  {'Subitems': itemlist1}
]);

print(newlist.toString());

CodePudding user response:

The below code gives you the required output.

var itemlist1 = [
      {"p_id": "a101", "model": "M-Plaz", "price": 2500},
      {"p_id": "a101", "model": "Z-Plaz", "price": 3500},
      {"p_id": "a102", "model": "M-Neo", "price": 1560},
      {"p_id": "a102", "model": "N-Neo1", "price": 3600}
    ];

    var newlist = groupBy(itemlist1, (Map obj) => obj['p_id']);

    var requiredOutput = [
      {"Subitems": []}
    ];
    newlist.forEach((k, v) => {
          requiredOutput[0]["Subitems"]!.add({"p_id": k, "items": v})
        });

    print(requiredOutput);

Note: Add import "package:collection/collection.dart"; line in imports.

CodePudding user response:

I would recommend you not to work directly on a Map<String, dynamic> you get from Json. You could rather define a Model for your Products.

class Product {
  final String pId;
  final String model;
  final double price;
  
  Product({
    required this.pId,
    required this.model,
    required this.price
  });
  
  Product.fromJson(Map<String, dynamic> json):
    pId = json['p_id'] as String,
    model = json['model'] as String,
    price = json['price'] as double;
  
  Map<String, dynamic> toJson() => {
    'p_id': pId,
    'model': model,
    'price': price
  };
}

Note: Have a look at the json_serializable & freezed packages too.

Then, grouping your Products by id gets easier:

void main() {
  final List<Product> list =[
    {"p_id": "a101", "model": "M-Plaz","price": 2500},
    {"p_id": "a101", "model": "Z-Plaz","price": 3500},
    {"p_id": "a102", "model": "M-Neo", "price": 1560},
    {"p_id": "a102", "model": "N-Neo1","price": 3600},
  ].map((item) => Product.fromJson(item)).toList();
  final Map<String, List<Product>> processed = list.fold({}, (pMap, product) => {
    ...pMap,
    product.pId: [...(pMap[product.pId] ?? []), product]
  });
  print(jsonEncode(processed));
}

Console log:

{"a101":[{"p_id":"a101","model":"M-Plaz","price":2500},{"p_id":"a101","model":"Z-Plaz","price":3500}],"a102":[{"p_id":"a102","model":"M-Neo","price":1560},{"p_id":"a102","model":"N-Neo1","price":3600}]}

CodePudding user response:

Try this code snippet:

 var itemlist1=[
          {"p_id": "a101", "model": "M-Plaz","price": 2500},
          {"p_id": "a101", "model": "Z-Plaz","price": 3500},
          {"p_id": "a102", "model": "M-Neo", "price": 1560},
          {"p_id": "a102", "model": "N-Neo1","price": 3600}];

 // list for subItems
  var subItems = [];
  itemlist1.forEach((item){
     
     // check for subitem for p_id already exists or not
     List items =  subItems.where((i)=>
        i['p_id'] == (item)['p_id']
      ).toList();
    
      if(items.length > 0 ){
        (items[0] as Map<String,dynamic>)['items'].add(item);
      }else {
          Map newMap = Map<String,dynamic>();
          newMap.putIfAbsent('p_id', () => (item)['p_id'].toString());
          List newList = [item];
          newMap.putIfAbsent('items', () => newList);
          subItems.add(newMap);
      } 
    });
    
 
   var output = [
      {"Subitems": subItems}
    ];

  print(output);
  • Related