我如何将一个json数组划分为2个不同的json数组

时间:2018-08-30 08:44:11

标签: javascript arrays json

嗨,我有一个json响应

   response.data: [{
        "id": 1,
        "datefrom": "2018-08-30 11:21:25",
        "dateto": "2018-08-31 11:21:25",
    }, {
         "id": 2,
        "datefrom": "2018-08-30 11:21:25",
        "dateto": "2018-08-31 11:21:25",
    }, {
        "id": 3,
        "datefrom": "2018-09-01 11:21:25",
        "dateto": "2018-09-03 11:21:25",
    }, {
         "id": 4,
        "datefrom": "2018-09-02 11:21:25",
        "dateto": "2018-09-03 11:21:25",
    }, {
         "id": 5,
        "datefrom": "2018-09-03 11:21:25",
        "dateto": "2018-09-04 11:21:25",
    }]

如果datefrom <“ 2018-08-31 11:21:25”

,我需要根据datefrom列将此json数据分为2个

应该是这样的:

json1: [{
        "id": 1,
        "datefrom": "2018-08-30 11:21:25",
        "dateto": "2018-08-31 11:21:25",
    }, {
         "id": 2,
        "datefrom": "2018-08-30 11:21:25",
        "dateto": "2018-08-31 11:21:25",
    }]

json2:[{
        "id": 3,
        "datefrom": "2018-09-01 11:21:25",
        "dateto": "2018-09-03 11:21:25",
    }, {
         "id": 4,
        "datefrom": "2018-09-02 11:21:25",
        "dateto": "2018-09-03 11:21:25",
    }, {
         "id": 5,
        "datefrom": "2018-09-03 11:21:25",
        "dateto": "2018-09-04 11:21:25",
    }]

我该如何使用JavaScript?

我尝试过

if(datediff<120000){


        values.push(response.data[i]['datefrom']);
         Object.assign(values, {datefrom: response.data[i]['datefrom']});
        values.push(response.data[i]['checkout']);
         Object.assign(values, {dateto: response.data[i]['dateto']});
        console.log(values)
        $rootScope.expesscheckindata1= values;

      else{
        values1.push(response.data[i]['datefrom']);
        Object.assign(values1, {datefrom: response.data[i]['datefrom']});
        values1.push(response.data[i]['checkout']);
        Object.assign(values1, {dateto: response.data[i]['dateto']});
         console.log(values1)
        $rootScope.expesscheckindata2= values1;

,但在执行操作时,数据将作为下一个数据附加到 [“ 2018-08-30 11:21:25”,“ 2018-08-31 11:21:25”,“ 2018-08-29 00:00:00”,“ 2018-08-30 00:00: 00“,日期从:” 2018-08-29 00:00:00“,日期到:” 2018-08-30 00:00:00“]

7 个答案:

答案 0 :(得分:1)

这是您想要的功能

var j=[{
        "id": 1,
        "datefrom": "2018-08-30 11:21:25",
        "dateto": "2018-08-31 11:21:25",
    }, {
        "id": 2,
        "datefrom": "2018-08-30 11:21:25",
        "dateto": "2018-08-31 11:21:25",
    }, {
        "id": 3,
        "datefrom": "2018-09-01 11:21:25",
        "dateto": "2018-09-03 11:21:25",
    }, {
         "id": 4,
        "datefrom": "2018-09-02 11:21:25",
        "dateto": "2018-09-03 11:21:25",
    }, {
         "id": 5,
        "datefrom": "2018-09-03 11:21:25",
        "dateto": "2018-09-04 11:21:25",
    }]


var a1=[],a2=[]
function splitingByDate(_date){
  j.map(function(a){
    (new Date(a.datefrom)<new Date(_date))?a1.push(a):a2.push(a)
  })
}
//calling the function

splitingByDate("2018-08-31 11:21:25")

//Printing the Output
console.log(a1)
console.log("-------------------------")
console.log(a2)

答案 1 :(得分:1)

您可以比较response.data [index] .date的值,然后根据值将其添加到空数组json1json2

var response = {};
response.data = [{
        "id": 1,
        "datefrom": "2018-08-30 11:21:25",
        "dateto": "2018-08-31 11:21:25",
    }, {
         "id": 2,
        "datefrom": "2018-08-30 11:21:25",
        "dateto": "2018-08-31 11:21:25",
    }, {
        "id": 3,
        "datefrom": "2018-09-01 11:21:25",
        "dateto": "2018-09-03 11:21:25",
    }, {
         "id": 4,
        "datefrom": "2018-09-02 11:21:25",
        "dateto": "2018-09-03 11:21:25",
    }, {
         "id": 5,
        "datefrom": "2018-09-03 11:21:25",
        "dateto": "2018-09-04 11:21:25",
    }];
    
    var json1=[];
    var json2=[];
    
for(i=0; i<response.data.length; i++){
if(response.data[i].datefrom < '2018-08-31'){
json1.push(response.data[i]);
}else{
json2.push(response.data[i]);
}
}


console.log("JSON1: ");
console.log(json1);
console.log("JSON2: ");
console.log(json2);

答案 2 :(得分:0)

为自己提供一个遍历数组的方法,如果日期超过了要求的日期,则返回索引

现在将这个索引与response.data.slice(index,response.data.length)一起使用将返回array2,与slice(0,index)相同的将返回array1

答案 3 :(得分:0)

您可以使用过滤器:

data = [
    {
        id: 1,
        datefrom: '2018-08-30 11:21:25',
        dateto: '2018-08-31 11:21:25',
    },
    {
        id: 2,
        datefrom: '2018-08-30 11:21:25',
        dateto: '2018-08-31 11:21:25',
    },
    {
        id: 3,
        datefrom: '2018-09-01 11:21:25',
        dateto: '2018-09-03 11:21:25',
    },
    {
        id: 4,
        datefrom: '2018-09-02 11:21:25',
        dateto: '2018-09-03 11:21:25',
    },
    {
        id: 5,
        datefrom: '2018-09-03 11:21:25',
        dateto: '2018-09-04 11:21:25',
    },
];

console.log(
    "smaller",
    data.filter(
        item=>
            item.datefrom.localeCompare("2018-08-31 11:21:25")===-1
    )
);
console.log(
    "bigger",
    data.filter(
        item=>
            item.datefrom.localeCompare("2018-08-31 11:21:25")!==-1
    )
);

const splitByDate = (date,array) =>
  array.reduce(
    (result,item)=>{
      (new Date(item.datefrom).getTime())<date.getTime()
        ? result[0].push(item)
        : result[1].push(item);
      return result;
    },
    [[],[]]//initial result
  );
const [low,high] = splitByDate(new Date('2018-08-31 11:21:25'),data)
//12 hours from now:
//const [low,high] = splitByDate(
//    new Date(new Date().getTime()+12*60*60*1000)
//    ,data
//);
console.log(
  "date compare low",
  low,
  "date compare high",
  high
);

答案 4 :(得分:0)

 

const data = [
    {
        id: 1,
        datefrom: '2018-08-30 11:21:25',
        dateto: '2018-08-31 11:21:25',
    },
    {
        id: 2,
        datefrom: '2018-08-30 11:21:25',
        dateto: '2018-08-31 11:21:25',
    },
    {
        id: 3,
        datefrom: '2018-09-01 11:21:25',
        dateto: '2018-09-03 11:21:25',
    },
    {
        id: 4,
        datefrom: '2018-09-02 11:21:25',
        dateto: '2018-09-03 11:21:25',
    },
    {
        id: 5,
        datefrom: '2018-09-03 11:21:25',
        dateto: '2018-09-04 11:21:25',
    },
];
var json1 = [];
var json2 = [];
$.each(data, function(index, value) {
    if (value.datefrom == '2018-08-31 11:21:25') {
        json1.push(value);
    } else {
        json2.push(value);
    }
});
console.log("one",json1);
console.log("two",json2);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

答案 5 :(得分:0)

您可以使用数组的reduce()方法和Date.parse()来检查当前日期的日期值以获得所需的结果。

演示

const data=[{"id":1,"datefrom":"2018-08-30 11:21:25","dateto":"2018-08-31 11:21:25"},{"id":2,"datefrom":"2018-08-30 11:21:25","dateto":"2018-08-31 11:21:25"},{"id":3,"datefrom":"2018-09-01 11:21:25","dateto":"2018-09-03 11:21:25"},{"id":4,"datefrom":"2018-09-02 11:21:25","dateto":"2018-09-03 11:21:25"},{"id":5,"datefrom":"2018-09-03 11:21:25","dateto":"2018-09-04 11:21:25"}];

let result = data.reduce((r, o) => {
    let index = Date.parse(o.datefrom) < Date.now() ? 0 : 1;
    r[index].push(o);
    return r;
}, [ [],[] ]);

console.log(result[0],'\n\n',result[1])
.as-console-wrapper {max-height: 100% !important;top: 0;}

答案 6 :(得分:0)

首先,您必须解析JSON以使其成为javascript对象。

这是通过内置的javascript类JSON完成的:

var jsObj = JSON.parse(json);

由于json的根是json数组,因此jsObj将是javascript数组。

JavaScript内置数组具有用于操纵和遍历它们的实用方法。

Javascript Arrays

通过阅读文档,您将了解Array.prototype.findIndexArray.prototype.slice

Array.prototype.findIndex以谓词作为参数,该函数将对数组中的每个元素执行,并且在对要查找的元素进行加密时应返回true:

var refTime = new Date('2018-08-31 11:21:25').getTime();
var index = jsObj.findIndex(function(elem) {
    var currentTime = new Date(elem.datefrom).getTime();
    // will return true when encoutering the first occurence of a date > refDate
    return currentTime > refTime;

});
let result = jsObj;
// if the index returned by findIndex is equal to -1 it means that no element satisfying the predicate were found so result will be the original array (jsObj)
if (index !== -1) {
    // we have found an index around which we want to divide the array so result will be an array containing the two resulting arrays
    result = [jsObj.slice(0, index), jsObj.slice(index)];
}

请注意,此代码期望数组中的元素按datefrom的升序排序。如果不是这种情况,则还需要先按升序对数组进行排序,这是通过以下方式完成的:

jsObj.sort((e1, e2) => {
    var time1 = new Date(e1.datefrom).getTime();
    var time2 = new Date(e2.datefrom).getTime();
    return Math.sign(time1 - time2);
});

我确定其他人已指出您如何迭代数组并根据您的条件通过推入数组来填充结果数组,但这只是对如何通过标准数组操作“划分数组”的纯粹回应工具。

Working fiddle here(检查控制台以查看结果)

查看Array.prototype.sliceArray.prototype.findIndexArray.prototype.sort