简体   繁体   English

从具有相同键的对象创建一个新数组

[英]Create a new array from object with same keys

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.在下面的示例中,我有一个数据数组,我从服务器获取它作为对 API 的响应,我想使用call_id作为键将响应索引到新数组中。

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

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)您将使用.reduce() ,它使用两个值作为参数,一个累加器(在这种情况下是一个对象)和正在迭代的当前值(在这种情况下,数组中的每个对象)

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.每次迭代您检查累加器以查看当前迭代对象的call_id是否已经存在,如果存在,那么您只需将对象推入其中,如果不存在,则创建一个以call_id为键的新对象。

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

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

You can initialize an object, and for every item in the array check if object[item.call_id] .您可以初始化一个对象,并检查数组中的每个项目是否为object[item.call_id] If it doesn't, assign that new key the value of [item] (starting off the array).如果不是,则为该新键分配[item]的值(从数组开始)。 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.如果是,只需获取object[call_id]的值,将当前项推入该数组,然后将object[call_id]重新分配给更新后的数组。 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM