簡體   English   中英

將具有相同數量的鍵值對的JSON對象組合在一起,並附加主鍵值

[英]Combine JSON Objects with same number of key-value pair, appendinging the main key values

我在幾年前編寫的解析器程序之一中關注了JSON對象。

{"Date":"19/02/16","Narration":"NEFT DR-BARB0PIMPAL-Govt. NE","Chq":{"/Ref":{"No":{"":"N050160130709970"}}},"Value Dt":"19/02/16","Withdrawal Amt":{"":"8,000.00"},"Deposit Amt":{"":""},"Closing Balance":"24,114.95"}

{"Date":"","Narration":"TBANK, MUM-N050160130709970","Chq":{"/Ref":{"No":{"":""}}},"Value Dt":"","Withdrawal Amt":{"":""},"Deposit Amt":{"":""},"Closing Balance":""}

基本上,以上條目應該是單個記錄。 但是,由於換行器使解析器將其視為新記錄。

我嘗試通過將對象轉換為數組來映射它們。 但是,該地圖的結果為以下JSON對象。

{"Date":"19/02/16"}{"Date":""},{"Narration":"NEFT DR-BARB0PIMPAL-Govt. NE"}{"Narration":"TBANK, MUM-N050160130709970"},{"Chq":{"/Ref":{"No":{"":"N050160130709970"}}}}{"Chq":{"/Ref":{"No":{"":""}}}},{"Value Dt":"19/02/16"}{"Value Dt":""},{"Withdrawal Amt":{"":"8,000.00"}}{"Withdrawal Amt":{"":""}},{"Deposit Amt":{"":""}}{"Deposit Amt":{"":""}},{"Closing Balance":"24,114.95"}{"Closing Balance":""}

我需要的是一個合並/合並的數組OR對象,其主鍵保持不變,並且值聯接/附加到第一個元素(第二個對象在此之后被丟棄)。

例如。

{"Date":"19/02/16","Narration":"NEFT DR-BARB0PIMPAL-Govt.NETBANK","Chq":{"/Ref":{"No":{"":"N050160130709970MUM-N050160130709970"}}},"Value Dt":"19/02/16","Withdrawal Amt":{"":"8,000.00"},"Deposit Amt":{"":""},"Closing Balance":"24,114.95"}

你們能建議我可以直接合並/合並源JSON對象還是合並具有上述效果的數組?

顯然,您需要知道這些對象的順序,所以我假設您有一個它們的數組,所有這些數組都從JSON解析為JavaScript對象。

其次,我假設可以通過一個空余額字段來識別作為上一個記錄的延續的記錄(例如-根據需要進行調整)。

這是執行合並的代碼:

 function mergeSplitObjects(arr) { return arr.filter( (curr, i) => { // Does this look like a split-off object that should be merged with previous? if (curr["Closing Balance"] !== "") { // Adapt condition as needed return true; // No, it is a real record } arr[i-1]["Narration"] += curr["Narration"]; // Merge, and filter curr out }); } // Sample data with 3 records. First two should be merged let arr = [ {"Date":"19/02/16","Narration":"NEFT DR-BARB0PIMPAL-Govt. NE","Chq":{"/Ref":{"No":{"":"N050160130709970"}}},"Value Dt":"19/02/16","Withdrawal Amt":{"":"8,000.00"},"Deposit Amt":{"":""},"Closing Balance":"24,114.95"}, {"Date":"","Narration":"TBANK, MUM-N050160130709970","Chq":{"/Ref":{"No":{"":""}}},"Value Dt":"","Withdrawal Amt":{"":""},"Deposit Amt":{"":""},"Closing Balance":""}, {"Date":"20/02/16","Narration":"ATM NYC 13","Chq":{"/Ref":{"No":{"":"N050160130709971"}}},"Value Dt":"20/02/16","Withdrawal Amt":{"":"1,000.00"},"Deposit Amt":{"":""},"Closing Balance":"23,114.95"}, ]; arr = mergeSplitObjects(arr); console.log(arr); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

我將使用類似於Object.assign的函數。

就像Trincot一樣,我認為Closing Balance是合並記錄的指示。

var splitRegExp = /[a-z],\s*[a-z]/gi;
function deepMergingAssign(obj /*, sourceObjects*/){
    'use strict';
    if (obj == null) throw new TypeError('Cannot convert undefined or null to object');
    var final = Object(obj);
    [].slice.call(arguments, 1).forEach(function(arg){
        if(arg != null) {
            for(var prop in arg) {
                if (typeof arg[prop] === 'object'&&typeof final[prop] === 'object') {
                    // recursively call this function
                    arg[prop] = deepMergingAssign(final[prop],arg[prop]);
                    delete arg[prop]
                }
                else if(Object.prototype.hasOwnProperty.call(arg,prop)) {
                    // append the new values to the existing value.
                    var currentValue = final[prop].split(splitRegExp);
                    var incomingValue = arg[prop].split(splitRegExp);
                    incomingValue.forEach( function(val){
                        if (val! == '' && currentValue.indexOf(val) === -1) currentValue.push(val);
                    });
                    final[prop] = currentValue.join(', ');
                }
            }
        }
    });
    return final;
}

function mergeRecords(records){
    var toMerge = [];
    var mergedData = []
    for (var i=0; i<records.length; i++){
        // Change this condition as needed
        if (records[i]['Closing Balance'] !== '' && toMerge.length>0){
            // Create a merged record and reset the toMerge variable
            mergedData.push(deepMergingAssign.apply(null, toMerge));
            toMerge = [records[i]];
        }
        else {
            // This record should be merged with the previous.
            toMerge.push(records[i]);
        }
    }
    // Merge the last records stored in the array
    mergedData.push(deepMergingAssign.apply(null, toMerge));

    return mergedData;
}

var allRecords = [
    {"Date":"19/02/16","Narration":"NEFT DR-BARB0PIMPAL-Govt. NE","Chq":{"/Ref":{"No":{"":"N050160130709970"}}},"Value Dt":"19/02/16","Withdrawal Amt":{"":"8,000.00"},"Deposit Amt":{"":""},"Closing Balance":"24,114.95"},
    {"Date":"","Narration":"TBANK, MUM-N050160130709970","Chq":{"/Ref":{"No":{"":""}}},"Value Dt":"","Withdrawal Amt":{"":""},"Deposit Amt":{"":""},"Closing Balance":""},
    {"Date":"21/02/16","Narration":"TBANK","Chq":{"/Ref":{"No":{"":"N050160130709971"}}},"Value Dt":"21/02/16","Withdrawal Amt":{"":""},"Deposit Amt":{"":"2,000.00"},"Closing Balance":"26,114.95"},
    {"Date":"22/02/16","Narration":"TBANK","Chq":{"/Ref":{"No":{"":"N050160130709972"}}},"Value Dt":"22/02/16","Withdrawal Amt":{"":"5,750.00"},"Deposit Amt":{"":"1,000.00"},"Closing Balance":"21,364.95"},
    {"Date":"","Narration":"TBANK, MUM-N050160130709972","Chq":{"/Ref":{"No":{"":""}}},"Value Dt":"","Withdrawal Amt":{"":""},"Deposit Amt":{"":""},"Closing Balance":""}
]

var merged = mergeRecords(allRecords);
/* [
    record 1+2,
    record 3,
    record 4+5
   ] */

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM