如果Array包含Array中的特定键扩展Object,则将Object推入Array

时间:2016-08-19 09:24:35

标签: javascript arrays object

我映射一个Object并运行一个数组,如果它们具有相同的键我为该用户创建了一个Object。

我现在想要实现的是,在将用户Object推入数组之前,我想检查该用户是否已经存在于该数组中,如果是,只需按日期扩展对象,否则将对象推入数组。

但我目前的结果是一个空数组,我不知道如何克服这个问题。  什么不起作用被提到作为小提琴中的评论:

var allUser = [
    { "userLogin": "t.test" },
    { "userLogin": "a.test" },
    { "userLogin": "b.test" }
];

var userFound = ["t.test","a.test"]

var requestByRange = [{
    "dateFrom": "2016-09-01",
    "dateTo": "2016-09-13",
    "userID": "13",
    "userLogin": "t.test"
}, {
    "dateFrom": "2016-09-09",
    "dateTo": "2016-09-16",
    "userID": "16",
    "userLogin": "t.test"
}, {
    "dateFrom": "2016-09-08",
    "dateTo": "2016-09-23",
    "userID": "16",
    "userLogin": "a.test"
}];

var usersToDisplay = [];
        if (userFound.length != 0 && requestByRange != undefined) {
            allUser.map(function (value, index) {
                for (var i = 0; i < userFound.length; i++) {
                    if (value.userLogin === userFound[i]) {
                        for(var key in requestByRange){
                            if(requestByRange.hasOwnProperty(key) && key.indexOf(value.userLogin) === -1){
                                //console.log("yipia")
                                //console.log(key)
                                //console.log(requestByRange[key].dateFrom)
                                //console.log(requestByRange[key].dateTo)
                                //console.log(value)

                                var userToDisplay = {
                                    userLogin: value.userLogin,
                                    dateFrom: [requestByRange[key].dateFrom],
                                    dateTo: [requestByRange[key].dateTo]
                                }

                                /*
                                THE PART BELOW DOES NOT WORK!!!
                                If userToDisplay.userLogin is already in usersToDisplay
                                Push the dates in dateFrom and dateTo, otherwise 
                                push the userToDisplay Object into the usersToDisplay array
                                
                                Current output is an empty array
                                */
                               console.log("--------------------------------------")
                                for(var usersKey in usersToDisplay){
                                    console.log(usersToDisplay[usersKey])
                                    if(usersToDisplay.hasOwnProperty(usersKey) && usersKey.indexOf(usersToDisplay.userLogin) === -1){
                                        console.log("ja");
                                        console.log(usersToDisplay[usersKey])
                                        usersToDisplay[usersKey].dateFrom.push(userToDisplay.dateFrom)
                                        usersToDisplay[usersKey].dateTo.push(userToDisplay.dateTo)
                                    }else{
                                        console.log("--------------------------------------")
                                        usersToDisplay.push(userToDisplay);
                                    }
                                }
                            }
                        }
                    }
                }
            });
        }
        
        console.log(usersToDisplay)

Fiddle

结果应该是一个数组,其中包含一个对象,其中每个userLogin只存在一次,并且所有dateFrom & dateTo作为该对象中属于该用户的数组。

  var usersToDisplay = [
    {
      "dateFrom": ["2016-09-01", "2016-09-09"],
      "dateTo": ["2016-09-13, "2016-09-16],
      "userID": "16",
      "userLogin": "t.test"
    }
   ]

2 个答案:

答案 0 :(得分:1)

您可以创建地图并将其用作对结果集中项目的引用。

&#13;
&#13;
var allUser = [{ "userLogin": "t.test", }, { "userLogin": "a.test", }, { "userLogin": "b.test", }],
    userFound = ["t.test", "a.test"],
    requestByRange = [{ "dateFrom": "2016-09-01", "dateTo": "2016-09-13", "userID": "13", "userLogin": "t.test" }, { "dateFrom": "2016-09-09", "dateTo": "2016-09-16", "userID": "16", "userLogin": "t.test" }, { "dateFrom": "2016-09-08", "dateTo": "2016-09-23", "userID": "16", "userLogin": "a.test" }],
    map = new Map,
    result = allUser.map(function (a) {
        var o = {};
        Object.assign(o, a);
        map.set(a.userLogin, o);
        return o;
    });

requestByRange.forEach(function (a) {
    var o = map.get(a.userLogin);
    if (!o) {
        o = {};
        Object.assign(o, a);
        map.set(a.userLogin, o);
        result.push(o);
    }
    o.dateFrom = o.dateFrom || [];
    o.dateFrom.push(a.dateFrom);
    o.dateTo = o.dateTo || [];
    o.dateTo.push(a.dateTo);
    o.userID = o.userID || a.userID;
});

console.log(result);
&#13;
&#13;
&#13;

答案 1 :(得分:0)

这是你需要的吗?我首先映射了范围数组,因此我们不必嵌套这么多循环。这假设userFound中的每个用户在范围数组中至少有一条记录,如果不存在范围,您可以轻松编辑它以使用默认值。

    // Your inputs
var allUser = [
        { "userLogin": "t.test" },
        { "userLogin": "a.test" },
        { "userLogin": "b.test" }
    ],
    userFound = ["t.test","a.test"],
    requestByRange = [{
        "dateFrom": "2016-09-01",
        "dateTo": "2016-09-13",
        "userID": "13",
        "userLogin": "t.test"
    }, {
        "dateFrom": "2016-09-09",
        "dateTo": "2016-09-16",
        "userID": "16",
        "userLogin": "t.test"
    }, {
        "dateFrom": "2016-09-08",
        "dateTo": "2016-09-23",
        "userID": "16",
        "userLogin": "a.test"
    }],
    // Helpers
    rangesPerUser = requestByRange.reduce(function( map, record ) {
        var login = record.userLogin;
        if (!map[login]) map[login] = [];
        map[login].push(record);
        return map;
    }, {}),
    result = [];

allUser.forEach(function( user ) {
    var login = user.userLogin,
        record;
    if (userFound.indexOf(login) !== -1) {
        record = {
            'userID' : rangesPerUser[login][0].userID,
            'userLogin' : login,
            'dateFrom' : [],
            'dateTo' : []
        };
        rangesPerUser[login].forEach(function( range ) {
            record.dateFrom.push(range.dateFrom);
            record.dateTo.push(range.dateTo);
        });
        result.push(record);
    }
});