使用相同的键从对象创建新数组

时间:2020-02-20 19:13:19

标签: javascript arrays

我有一个对象数组。我想将对象合并到单个数组中,并使用值之一作为键。在下面的示例中,我有一个数据数组,该数据数组是从服务器获取的,作为对API的响应,并且我想使用call_id作为键将响应索引到一个新数组中。

我尝试过:
data.map(function(index, elem) {responses[index.call_id] = index;})
但这显然只能得到最后一个数组,并添加一个[]会给我一个错误

当前数组:

[
    {
        "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"
    }
]

预期结果

[{
    '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"
        }
    ]  
}]

3 个答案:

答案 0 :(得分:3)

我认为您想要类似下面的内容,对吗?

如果是的话,让我解释一下:
您将使用.reduce(),它使用两个值作为参数,一个累加器(在这种情况下是一个对象)和正在迭代的当前值(在这种情况下,数组中的每个对象)

每次迭代都检查累加器,以查看当前迭代对象的call_id是否已经存在(如果存在),因此只需将对象推入其中,否则,请使用{{ 1}}作为密钥。

注意:我使用属性较少的数组只是为了更好地可视化代码

call_id

答案 1 :(得分: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);

答案 2 :(得分:0)

您可以初始化一个对象,并检查数组中的每个项目是否为object[item.call_id]。如果不是,则为该新键分配[item]的值(从数组开始)。如果是这样,只需获取object[call_id]的值,将当前项推入该数组,然后将object[call_id]重新分配给更新的数组。然后,如果希望将其作为数组,则将其包装在方括号中。

通过首先创建对象,您可以避免使用嵌套循环或减少嵌套循环的使用,这在数据集增长的时间效率方面是无效的。

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()