简体   繁体   English

如何更改对象内部的数组

[英]How to alter array inside an object

I have this JSON 我有这个JSON

{
    "Entries": [{
        "JobId": 7322,
        "DayOfTheWeek": "Wednesday",
        "Punchouts": ["2016-05-05T09:50:00", "2016-05-05T12:20:00"],
        "PunchIns": ["2016-05-05T10:10:00", "2016-05-05T12:40:00"]
    }, {
        "JobId": 7322,
        "DayOfTheWeek": "Thursday",
        "Punchouts": ["2016-05-05T09:50:00"],
        "PunchIns": ["2016-05-05T09:40:00"]
    }]
}

I would change it to 我将其更改为

{
    "Entries": [{
        "JobId": 7322,
        "DayOfTheWeek": "Wednesday",
        "Punchout1": "2016-05-05T09:50:00",
        "Punchout2": "2016-05-05T12:20:00",
        "PunchIn1": "2016-05-05T10:10:00",
        "PunchIn2": "2016-05-05T12:40:00"
    }, {
        "JobId": 7322,
        "DayOfTheWeek": "Thursday",
        "Punchout1": "2016-05-05T09:50:00",
        "Punchout2": "",
        "PunchIn1": "2016-05-05T09:40:00"
        "PunchIn2": "",
    }]
}

I would like to condense the array. 我想浓缩一下数组。 The array will have max length = 2. Even if the length is 0 / 1, the array should have empty string for PunchOut1/PunchOut2 and PunchIn1/PunchIn2 该数组的最大长度=2。即使长度为0/1,该数组对于PunchOut1 / PunchOut2PunchIn1 / PunchIn2也应具有空字符串

Implemented it like this. 这样实现。

for (var i = 0; i < data.Entries.length; i++) {
    var entry = data.Entries[i];
    if (entry["Punchouts"].length == 0) {
        entry["PunchOut1"] = "";
        entry["PunchOut2"] = "";
    }
    if (entry["Punchouts"].length == 1) {
        entry["PunchOut1"] = entry["Punchouts"][0];
        entry["PunchOut2"] = "";
    }
    if (entry["Punchouts"].length == 2) {
        console.log("in");
        entry["PunchOut1"] = entry["Punchouts"][0];
        entry["PunchOut2"] = entry["Punchouts"][1];
    }
    delete entry["Punchouts"];
    // do same for PunchIns.
}

Fiddle: https://jsfiddle.net/codeandcloud/rpgx28gy/ 小提琴: https : //jsfiddle.net/codeandcloud/rpgx28gy/

What would be a more optimized (preferably lodash) way? 什么是更优化(最好是破折号)的方式?

var remappedEntries = _.map(data.Entries, e => ({
    JobId: e.JobId,
    DayOfTheWeek: e.DayOfTheWeek,
    PunchOut1: e.PunchOuts[0] || "",
    PunchOut2: e.PunchOuts[1] || "",
    PunchIn1: e.PunchIns[0] || "",
    PunchIn2: e.PunchIns[1] || "",
}));

This uses lodash's map, which iterates through every value and gives you an iteratee function that you can use to re-map that entry to another object. 这使用lodash的映射,该映射迭代每个值,并为您提供iteratee函数,您可以使用该函数将条目重新映射到另一个对象。 Note that you don't need to use lodash at all for this, you can use the Array.prototype.map function, assuming the browsers you aim for support it. 请注意,您根本不需要使用lodash,可以使用Array.prototype.map函数,假设您打算支持的浏览器也支持。 There are also shims that you can use. 您还可以使用垫片。

If instead of getting a copy you want to modify the exact same object, you can use _.forEach , or keep using map and discard the previous object. 如果要获取完全相同的对象而不是获取副本,则可以使用_.forEach ,或者继续使用map并丢弃先前的对象。 Really depends on your case. 真的取决于您的情况。

I've also used arrow functions which make it simpler, from every "e" to the object that you want to return. 我还使用了箭头函数,使它更简单,从每个“ e”到要返回的对象。 Alternatively, this could be a named or an anonymous function that takes one parameter. 或者,这可以是带有一个参数的命名函数或匿名函数。

For each punchout, I'm taking advantage that you can check an array even if it doesn't have an object there. 对于每次打孔,我都利用了即使数组中没有对象的情况也可以检查的数组。 In that case, it'll return undefined. 在这种情况下,它将返回未定义。 For those, we default them to empty string by using the conditional or, because undefined is considered "falsy" and so that expression will return the empty string. 对于这些,我们通过使用条件或将它们默认为空字符串,因为undefined被认为是“ falsy”,因此表达式将返回空字符串。 If it had a value, it'd use that one. 如果它有一个值,那就用那个。

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

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