skip to Main Content

I want to create new array of object from apiResponse which will contain all the areaCode for particular RouteId and InDirection.

apiResponse = [
            {
                "RouteId": "1",
                "InDirection": "1",
                "AreaCode": "41108",
                "LastModified": "2011-06-15 00:00:00.000"
            },
            {
                "RouteId": "1",
                "InDirection": "1",
                "AreaCode": "41109",
                "LastModified": "2011-06-15 00:00:00.000"
            },
            {
                "RouteId": "1",
                "InDirection": "1",
                "AreaCode": "41110",
                "LastModified": "2011-06-15 00:00:00.000"
            },
            {
                "RouteId": "1",
                "InDirection": "1",
                "AreaCode": "41111",
                "LastModified": "2011-06-15 00:00:00.000"
            },
            {
                "RouteId": "1",
                "InDirection": "2",
                "AreaCode": "41108",
                "LastModified": "2011-06-15 00:00:00.000"
            },
            {
                "RouteId": "1",
                "InDirection": "2",
                "AreaCode": "41109",
                "LastModified": "2011-06-15 00:00:00.000"
            },
            {
                "RouteId": "1",
                "InDirection": "2",
                "AreaCode": "411011",
                "LastModified": "2011-06-15 00:00:00.000"
            },
            {
                "RouteId": "2",
                "InDirection": "1",
                "AreaCode": "41112",
                "LastModified": "2011-06-15 00:00:00.000"
            },
            {
                "RouteId": "2",
                "InDirection": "1",
                "AreaCode": "41114",
                "LastModified": "2011-06-15 00:00:00.000"
            }]

For e.g. for RouteId = 1 and InDirection = 1, result should be

result = [{
             "RouteId": "1",
             "InDirection": "1",
             "AreaCode": ["41108", "41109", "41110", "41111"],
          }]

and the final result for the given apiResponse should be

finalResult = [{
             "RouteId": "1",
             "InDirection": "1",
             "AreaCode": ["41108", "41109", "41110", "41111"],
          },
          {
             "RouteId": "1",
             "InDirection": "2",
             "AreaCode": ["41108", "41109", "411011"],
          }
          {
             "RouteId": "2",
             "InDirection": "1",
             "AreaCode": ["41112", "41114"],
          }]

I tried to extract InDirection but couldn’t create desired array

apiResponse.filter(x => x.RouteId === '1' && x.InDirection === '1').map(x => x.AreaCode);

Can someone help me with this?

2

Answers


  1. Use reduce to get the final result.

    const transformedData = apiResponse.reduce((acc, val) => {
      const existingItem = acc.find(
        item => item.RouteId === val.RouteId && item.InDirection === val.InDirection
      );
      if (existingItem) {
        existingItem.AreaCode.push(val.AreaCode);
      } else {
        acc.push({ ...val, AreaCode: [val.AreaCode] });
      }
      return acc;
    }, []);
    
    
    Login or Signup to reply.
  2.  const res = apiResponse.map((e) => {
      const f = apiResponse.filter((f) => f.RouteId === e.RouteId);
      var r = {};
      f.forEach((e) => {
        r = {
          RouteId: e.RouteId,
          InDirection: e.InDirection,
          AreaCode: r.AreaCode?[...r.AreaCode, e.AreaCode]:[e.AreaCode],
        };
      });
    
      return r;
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search