0

I have a array of objects. I want to merge the objects into a single array and use one of the value as key. In the example below I have a data array which I'm getting from the server as a response to a API and I want to use call_id as a key to index the response into a new array.

I've tried:
data.map(function(index, elem) {responses[index.call_id] = index;})
but this obviously only gets the last array and adding a [] gives me an error

Current Array:

[
    {
        "id": 2,
        "survey_id": 1,
        "question_id": 2,
        "response": 1,
        "order_id": null,
        "customer_id": 1,
        "call_id": 108,
        "created_at": "2020-02-20 18:18:47",
        "updated_at": "2020-02-20 18:18:47",
        "question": "Do you want it gift wrapped?",
        "first_name": "Zain",
        "sid": "CA1564cda12b7e1364dc967538c7bdf617"
    },
    {
        "id": 1,
        "survey_id": 1,
        "question_id": 1,
        "response": 2,
        "order_id": null,
        "customer_id": 1,
        "call_id": 108,
        "created_at": "2020-02-20 18:18:32",
        "updated_at": "2020-02-20 18:18:32",
        "question": "Is your order confirmed?",
        "first_name": "Zain",
        "sid": "CA1564cda12b7e1364dc967538c7bdf617"
    },
    {
        "id": 1,
        "survey_id": 1,
        "question_id": 1,
        "response": 2,
        "order_id": null,
        "customer_id": 1,
        "call_id": 109,
        "created_at": "2020-02-20 18:18:32",
        "updated_at": "2020-02-20 18:18:32",
        "question": "Is your order confirmed?",
        "first_name": "Zain",
        "sid": "CA1564cda12b7e1364dc967538c7bdf617"
    }
]

Expected Result

[{
    '108': [
        {
            "id": 2,
            "survey_id": 1,
            "question_id": 2,
            "response": 1,
            "order_id": null,
            "customer_id": 1,
            "call_id": 108,
            "created_at": "2020-02-20 18:18:47",
            "updated_at": "2020-02-20 18:18:47",
            "question": "Do you want it gift wrapped?",
            "first_name": "Zain",
            "sid": "CA1564cda12b7e1364dc967538c7bdf617"
        },
        {
            "id": 1,
            "survey_id": 1,
            "question_id": 1,
            "response": 2,
            "order_id": null,
            "customer_id": 1,
            "call_id": 108,
            "created_at": "2020-02-20 18:18:32",
            "updated_at": "2020-02-20 18:18:32",
            "question": "Is your order confirmed?",
            "first_name": "Zain",
            "sid": "CA1564cda12b7e1364dc967538c7bdf617"
        }
    ], 
    '109' : [
        {
            "id": 1,
            "survey_id": 1,
            "question_id": 1,
            "response": 2,
            "order_id": null,
            "customer_id": 1,
            "call_id": 109,
            "created_at": "2020-02-20 18:18:32",
            "updated_at": "2020-02-20 18:18:32",
            "question": "Is your order confirmed?",
            "first_name": "Zain",
            "sid": "CA1564cda12b7e1364dc967538c7bdf617"
        }
    ]  
}]
2
  • 2
    And what have you tried so far? Commented Feb 20, 2020 at 19:14
  • data.map(function(index, elem) {responses[index.call_id] = index;}) but this obviously only gets the last array and adding a [] gives me an error. Commented Feb 20, 2020 at 19:16

3 Answers 3

3

I think that you want something like below, correct?

If yes, let me explain a little bit:
You will use .reduce(), it uses two values as parameter, an accumulator (in this case is an object) and the current value that is being iterated(in this case each object from the array)

Each iteration you check the accumulator to see if the call_id of the current iterated object already exists or not, if exists, so you just push the object into it, if not, create a new object with call_id as key.

Note: I'm using an array with less properties just for better visualization of the code

let arr = [{
    "id": 2,
    "call_id": 108,
    "sid": "CA1564cda12b7e1364dc967538c7bdf617"
  },
  {
    "id": 1,
    "call_id": 108,
    "sid": "CA1564cda12b7e1364dc967538c7bdf617"
  },
  {
    "id": 3,
    "call_id": 109,
    "sid": "CA1564cda12b7e1364dc967538c7bdf617"
  }
]


let result = arr.reduce(function(accObj, currentObj) {
  accObj[currentObj.call_id] = accObj[currentObj.call_id] || [];
  accObj[currentObj.call_id].push(currentObj);
  return accObj;
}, {}); //{} is the accumulator object

console.log(result);

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

Comments

2

var arr = [
    {
        "id": 2,
        "survey_id": 1,
        "question_id": 2,
        "response": 1,
        "order_id": null,
        "customer_id": 1,
        "call_id": 108,
        "created_at": "2020-02-20 18:18:47",
        "updated_at": "2020-02-20 18:18:47",
        "question": "Do you want it gift wrapped?",
        "first_name": "Zain",
        "sid": "CA1564cda12b7e1364dc967538c7bdf617"
    },
    {
        "id": 1,
        "survey_id": 1,
        "question_id": 1,
        "response": 2,
        "order_id": null,
        "customer_id": 1,
        "call_id": 108,
        "created_at": "2020-02-20 18:18:32",
        "updated_at": "2020-02-20 18:18:32",
        "question": "Is your order confirmed?",
        "first_name": "Zain",
        "sid": "CA1564cda12b7e1364dc967538c7bdf617"
    },
    {
        "id": 1,
        "survey_id": 1,
        "question_id": 1,
        "response": 2,
        "order_id": null,
        "customer_id": 1,
        "call_id": 109,
        "created_at": "2020-02-20 18:18:32",
        "updated_at": "2020-02-20 18:18:32",
        "question": "Is your order confirmed?",
        "first_name": "Zain",
        "sid": "CA1564cda12b7e1364dc967538c7bdf617"
    }
];

var obj = {}

arr.forEach(item => {
   if (!obj[item.call_id]) obj[item.call_id] = []
   obj[item.call_id].push(item)   
})

console.log(obj);

Comments

0

You can initialize an object, and for every item in the array check if object[item.call_id]. If it doesn't, assign that new key the value of [item] (starting off the array). If it does, simply get the value of object[call_id], push the current item into that array, then reassign object[call_id] to the updated array. Then wrap the whole thing in brackets if you want it to be an array.

By creating the object first your avoiding the use of nested loops or reduce which can be inefficient in terms of time efficiency should the dataset grow.

var arr = [
    {
        "id": 2,
        "survey_id": 1,
        "question_id": 2,
        "response": 1,
        "order_id": null,
        "customer_id": 1,
        "call_id": 108,
        "created_at": "2020-02-20 18:18:47",
        "updated_at": "2020-02-20 18:18:47",
        "question": "Do you want it gift wrapped?",
        "first_name": "Zain",
        "sid": "CA1564cda12b7e1364dc967538c7bdf617"
    },
    {
        "id": 1,
        "survey_id": 1,
        "question_id": 1,
        "response": 2,
        "order_id": null,
        "customer_id": 1,
        "call_id": 108,
        "created_at": "2020-02-20 18:18:32",
        "updated_at": "2020-02-20 18:18:32",
        "question": "Is your order confirmed?",
        "first_name": "Zain",
        "sid": "CA1564cda12b7e1364dc967538c7bdf617"
    },
    {
        "id": 1,
        "survey_id": 1,
        "question_id": 1,
        "response": 2,
        "order_id": null,
        "customer_id": 1,
        "call_id": 109,
        "created_at": "2020-02-20 18:18:32",
        "updated_at": "2020-02-20 18:18:32",
        "question": "Is your order confirmed?",
        "first_name": "Zain",
        "sid": "CA1564cda12b7e1364dc967538c7bdf617"
    },
    {
        "id": 1,
        "survey_id": 1,
        "question_id": 1,
        "response": 2,
        "order_id": null,
        "customer_id": 1,
        "call_id": 109,
        "created_at": "2020-02-20 18:18:32",
        "updated_at": "2020-02-20 18:18:32",
        "question": "Is your order confirmed?",
        "first_name": "Zain",
        "sid": "CA1564cda12b7e1364dc967538c7bdf617"
    },
    {
        "id": 1,
        "survey_id": 1,
        "question_id": 1,
        "response": 2,
        "order_id": null,
        "customer_id": 1,
        "call_id": 109,
        "created_at": "2020-02-20 18:18:32",
        "updated_at": "2020-02-20 18:18:32",
        "question": "Is your order confirmed?",
        "first_name": "Zain",
        "sid": "CA1564cda12b7e1364dc967538c7bdf617"
    },
    {
        "id": 1,
        "survey_id": 1,
        "question_id": 1,
        "response": 2,
        "order_id": null,
        "customer_id": 1,
        "call_id": 109,
        "created_at": "2020-02-20 18:18:32",
        "updated_at": "2020-02-20 18:18:32",
        "question": "Is your order confirmed?",
        "first_name": "Zain",
        "sid": "CA1564cda12b7e1364dc967538c7bdf617"
    },
    {
        "id": 1,
        "survey_id": 1,
        "question_id": 1,
        "response": 2,
        "order_id": null,
        "customer_id": 1,
        "call_id": 109,
        "created_at": "2020-02-20 18:18:32",
        "updated_at": "2020-02-20 18:18:32",
        "question": "Is your order confirmed?",
        "first_name": "Zain",
        "sid": "CA1564cda12b7e1364dc967538c7bdf617"
    },
    {
        "id": 1,
        "survey_id": 1,
        "question_id": 1,
        "response": 2,
        "order_id": null,
        "customer_id": 1,
        "call_id": 109,
        "created_at": "2020-02-20 18:18:32",
        "updated_at": "2020-02-20 18:18:32",
        "question": "Is your order confirmed?",
        "first_name": "Zain",
        "sid": "CA1564cda12b7e1364dc967538c7bdf617"
    },
    {
        "id": 1,
        "survey_id": 1,
        "question_id": 1,
        "response": 2,
        "order_id": null,
        "customer_id": 1,
        "call_id": 109,
        "created_at": "2020-02-20 18:18:32",
        "updated_at": "2020-02-20 18:18:32",
        "question": "Is your order confirmed?",
        "first_name": "Zain",
        "sid": "CA1564cda12b7e1364dc967538c7bdf617"
    }
]

const reorderArr = () =>{
  let myMap = {}
 
  arr.forEach(x=>{
    if (!myMap[x.call_id]){
       myMap[x.call_id] = [x]
    } else {
      var children = myMap[x.call_id]
      children.push(x)
      myMap[x.call_id] = children
    }  
  })
  
  console.log([myMap])
  
}

reorderArr()

Comments

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.