0

I am trying to flatten an array with multiple objects inside my array. It keeps flatting it into one single array. I want it to have multiple objects inside the array but also want everything flatten with the everything removed except the keys and the values.

This is the current array named "livedata".

    [
       {
           "earningsFileId": {
               "value": 1234
           },
           "paymentType": {
               "value": "Session",
               "errors": [
                   {
                       "id": 802462,
                       "message": "Invalid Combination",
                       "status": "Processing"
                   }
               ]
           },
           "detailStatus": {
               "value": "Processing"
           }
       },
       {
           "earningsFileId": {
               "value": 5678
           },
           "paymentType": {
               "value": "Session",
               "errors": [
                   {
                       "id": 802462,
                       "message": "Invalid Combination",
                       "status": "Processing"
                   }
               ]
           },
           "detailStatus": {
               "value": "Processing"
           }
       }
]

This is the output I am trying to achieve.

[
   {
       "earningsFileId": 1234,
       "paymentType": "Session",
       "detailStatus": "Processing"
   },
   {
        "earningsFileId": 1234,
        "paymentType": "Session",
        "detailStatus": "Processing"
    }
]


data = [];
   Object.values(livedata).map((value, keys) => {
     Object.keys(value).forEach((key) => {
       data[key] = livedata[keys][key]['value']
     })
   });

1 Answer 1

2

You need to create a new "flattened" object from each object in livedata. You can get each key-value pair using Object.entries and reduce that array to your desired object:

const livedata = [{
    "earningsFileId": {
      "value": 1234
    },
    "paymentType": {
      "value": "Session",
      "errors": [{
        "id": 802462,
        "message": "Invalid Combination",
        "status": "Processing"
      }]
    },
    "detailStatus": {
      "value": "Processing"
    }
  },
  {
    "earningsFileId": {
      "value": 5678
    },
    "paymentType": {
      "value": "Session",
      "errors": [{
        "id": 802462,
        "message": "Invalid Combination",
        "status": "Processing"
      }]
    },
    "detailStatus": {
      "value": "Processing"
    }
  }
];

const result = livedata.map(d =>
  Object.entries(d).reduce((acc, [k, v]) => {
    acc[k] = v.value;
    return acc;
  }, {}))
console.log(result);

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, would you happen to know why I am getting the error. Property 'value' does not exist on type '{}'
@Suzed it's had to tell why without the actual data you're running this on because it's working for the data you provided in the question. Does your livedata array have other entries that have empty objects or do some of the inner objects don't have the property value?
Yes they actually do. there are some values that are undefined or empty.
@Suzed in that case, instead of directly assigning the value of the inner object (acc[k] = v.value), you can add a check like the following inside the reduce function: if ('value' in v): acc[k] = v.value;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.