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

时间:2018-07-22 23:17:36

标签: javascript arrays json algorithm dom

我的问题是关于在嵌套数组中隔离一个数组元素,并将其用作JSON字段值,并将其同级数组元素用作字段值属性。

我的阵列:

“ 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"],
]

我想用以下格式制作一个JSON对象:

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

到目前为止我的尝试:

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

3 个答案:

答案 0 :(得分:1)

尝试reduce-插入按日期索引的对象,该对象的值是Set s(要进行重复数据删除),将每个项目中的数字添加到该对象中,然后遍历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);

答案 1 :(得分:0)

您还可以使用普通对象将日期存储为键,将值存储为Set。值应存储在集合中,以免重复。然后最后,使用Array.from函数将所有Set转换为Array。

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

答案 2 :(得分:0)

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