简体   繁体   English

隔离嵌套数组中的数组元素,并将其用作JSON字段值,并将其同级数组元素用作字段值

[英]Isolate an array element inside nested array and use it as the JSON field value with it's sibling array elements as a field value

My question is about isolating an array element inside nested array and use it as the JSON field value with it's sibling array elements as a field value properties. 我的问题是关于在嵌套数组中隔离数组元素,并将其用作JSON字段值,并将其同级数组元素用作字段值属性。

My Array: 我的阵列:

"S,No.", "id", "date", "time" “ S,No。”,“ id”,“ date”,“ time”

eachArray = [
    ["1", "4", "2018/07/05", "22:27:35"],
    ["2", "1", "2018/07/05", "22:28:02"],
    ["3", "3", "2018/07/22", "21:25:51"],
    ["4", "3", "2018/07/22", "21:25:59"],
    ["5", "2", "2018/07/22", "21:25:59"],
    ["6", "1", "2018/07/25", "22:36:55"],
    ["7", "1", "2018/07/25", "21:25:51"],
]

I want to make a JSON object with the format like: 我想用以下格式制作一个JSON对象:

jsonArray = {
    "2018/07/05" : ["4","1"],
    "2018/07/22" : ["3","2"],
    "2018/07/25" : ["1"]
}

My attempt so far: 到目前为止,我的尝试:

 eachArray = [ ["1", "4", "2018/07/05", "22:27:35"], ["2", "1", "2018/07/05", "22:28:02"], ["3", "3", "2018/07/22", "21:25:51"], ["4", "3", "2018/07/22", "21:25:59"], ["5", "2", "2018/07/22", "21:25:59"], ["6", "1", "2018/07/25", "22:36:55"], ["7", "1", "2018/07/25", "21:25:51"], ] var outerSet = []; var jsonArray = {}; eachArray.forEach((val, index) =>{ var outerLast = outerSet.pop(); var set = eachArray[index][1]; var dateHere = eachArray[index][3]; outerSet.push(set); if(index !== 0){ if(outerLast !== dateHere){ outerSet = []; jsonArray[dateHere] = set; return; } } outerSet.push(dateHere); jsonArray[dateHere] = outerSet; }); 

Try reduce -ing into an object indexed by date, whose values are Set s (to deduplicate), to which you add the number in each item, and then once you've gone through the eachArray , convert the Set s back to arrays: 尝试reduce到按日期索引的对象,该对象的值是Set (重复数据删除),在其中添加每个项目中的数字,然后在eachArray ,将Set转换回数组:

 const eachArray = [ ["1", "4", "2018/07/05", "22:27:35"], ["2", "1", "2018/07/05", "22:28:02"], ["3", "3", "2018/07/22", "21:25:51"], ["4", "3", "2018/07/22", "21:25:59"], ["5", "2", "2018/07/22", "21:25:59"], ["6", "1", "2018/07/25", "22:36:55"], ["7", "1", "2018/07/25", "21:25:51"], ] const itemsByDate = eachArray.reduce((a, [, num, date]) => { if (!a[date]) a[date] = new Set(); a[date].add(num); return a; }, {}); Object.entries(itemsByDate).forEach(([key, set]) => { itemsByDate[key] = [...set]; }); console.log(itemsByDate); 

You can also use a plain object to store date as key and values as a Set . 您还可以使用普通对象将日期存储为键,将值存储为Set Values should be stored on a set so that it won't have duplicates. 值应存储在集合中,以免重复。 Then at the end, convert all the Sets to Arrays using Array.from function. 然后最后,使用Array.from函数将所有Set转换为Array.from

eachArray = [
    ["1", "4", "2018/07/05", "22:27:35"],
    ["2", "1", "2018/07/05", "22:28:02"],
    ["3", "3", "2018/07/22", "21:25:51"],
    ["4", "3", "2018/07/22", "21:25:59"],
    ["5", "2", "2018/07/22", "21:25:59"],
    ["6", "1", "2018/07/25", "22:36:55"],
    ["7", "1", "2018/07/25", "21:25:51"],
];

var obj = {};
eachArray.forEach(arr => {
    if(!obj[arr[2]]){
        obj[arr[2]] = new Set();
    }
    obj[arr[2]].add(arr[1]);
});
Object.keys(obj).forEach(key => obj[key] = Array.from(obj[key]));
console.log(obj);

 const input = [ ["1", "4", "2018/07/05", "22:27:35"], ["2", "1", "2018/07/05", "22:28:02"], ["3", "3", "2018/07/22", "21:25:51"], ["4", "3", "2018/07/22", "21:25:59"], ["5", "2", "2018/07/22", "21:25:59"], ["6", "1", "2018/07/25", "22:36:55"], ["7", "1", "2018/07/25", "21:25:51"], ]; const output = input.reduce((accu, [, num2, date,]) => { if(!accu[date]) accu[date] = []; if(!accu[date].includes(num2)) accu[date].push(num2); return accu; }, {}); console.log(output); 

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

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