Mapping array of objects to an array of Objects with object and array

I have an object

   const response =  {
        "message": "story records found successfully",
        "result": [
            {
                "created_AT": "Thu, 13 Jan 2022 17:37:04 GMT",
                "created_BY": "avinash",
                "dateTime": "Thu, 13 Jan 2022 17:37:04 GMT",
                "deleted_BY": "",
                "flag": 0,
                "project_ID": "infobot1234",
                "stories": {
                    "steps": [
                        {
                            "intent": "test"
                        },
                        {
                            "action": "uttence_test_heading"
                        },
                        {
                            "intent": "fruits"
                        },
                        {
                            "intent": "testintent"
                        },
                        {
                            "action": "utter_txt1234"
                        },
                        {
                            "action": "uttence_test_heading"
                        },
                        {
                            "action": "utter_txt1234"
                        },
                        {
                            "intent": "test"
                        },
                        {
                            "intent": "fruits"
                        },
                        {
                            "action": "uttence_test_heading"
                        },
                        {
                            "intent": "my_localities12333qqqwq"
                        }
                    ],
                    "story": "happy path rdedd"
                },
                "stories_ID": "f5728c4f-3717-40c2-8419-265d5d59bfd1",
                "updated_BY": "",
                "user_ID": "av1234"
            }
        ],
        "status_code": 0
    }

From this I have to create a structure like, so my expected output is

const data = [
                  {intent: 'test', actions:['uttence_test_heading']},
                  {intent:'fruits', actions:[]},
                  {intent:"testintent", actions:["utter_txt1234","uttence_test_heading","utter_txt1234"]},
                  {intent:"test", actions:[]},
                  {intent:"fruits", actions:["uttence_test_heading"]},
                  {intent: "my_localities12333qqqwq", actions:[]}
                 ]

The closest that I could get was using response.result[0].stories.steps which gives me the result

[{
  intent: "test"
}, {
  action: "uttence_test_heading"
}, {
  intent: "fruits"
}, {
  intent: "testintent"
}, {
  action: "utter_txt1234"
}, {
  action: "uttence_test_heading"
}, {
  action: "utter_txt1234"
}, {
  intent: "test"
}, {
  intent: "fruits"
}, {
  action: "uttence_test_heading"
}, {
  intent: "my_localities12333qqqwq"
}]

How can I achieve my expected result do I have to use some condition or something like Object.keys and then apply any condition, because for each intent there is an action and if there is no action then it should be an empty array.

How can I achieve my expected result please guide !

>Solution :

You need to iterate through each array element using the reduce array method, and check if the element is an intent. If it is, push a new object to the result array, if it isn’t, just push a new element to the actions array of the last index intent using the .at() method:

const steps = [
  {"intent": "test"},
  {"action": "uttence_test_heading"},
  {"intent": "fruits"},
  {"intent": "testintent"},
  {"action": "utter_txt1234"},
  {"action": "uttence_test_heading"},
  {"action": "utter_txt1234"},
  {"intent": "test"},
  {"intent": "fruits"},
  {"action": "uttence_test_heading"},
  {"intent": "my_localities12333qqqwq"}
];

const result = steps.reduce((acc, e) => {
  if (Object.keys(e)[0] === "intent") {
    acc.push({ ...e, actions: [] });
  }else {
    acc.at(-1).actions.push(e.action)
  }
  
  return acc;
}, []);

console.log(result)

Leave a Reply