将两个json对象数据合并为一个,JSON转换为新的Single JSON,

时间:2019-03-19 07:33:00

标签: javascript jquery arrays json object

我有如下定义的对象:

var object = {
  "driver_data": [{
    "slot": 0,
    "rideCount":98
  }, {
    "slot": 30,
    "rideCount": 75
  }, {
    "slot": 100,
    "rideCount": 0
  }],
  "passenger_data": [{
    "slot": 0,
    "rideCount":33
  }, {
    "slot": 30,
    "rideCount": 56
  }, {
    "slot": 100,
    "rideCount": 37
  }]
}

想要像这样转换:

[{"Slot":0,"passenger_data":98,"driver_data":33},
 {"Slot":30,"passenger_data":75,"driver_data":56},
 {"Slot":100,"passenger_data":0,"driver_data":37}
]

我尝试了这个但是没用。

var slot1 = [],
  Dride = [],
  Pride = [];
var object = {
  "driver_data": [{
    "slot": 0,
    "rideCount": 98
  }, {
    "slot": 30,
    "rideCount": 75
  }, {
    "slot": 100,
    "rideCount": 0
  }],
  "passenger_data": [{
    "slot": 0,
    "rideCount": 33
  }, {
    "slot": 30,
    "rideCount": 56
  }, {
    "slot": 100,
    "rideCount": 37
  }]
}
object.driver_data.forEach(function(key) {
  slot1.push(key.slot);
  Pride.push(key.rideCount)
});
object.passenger_data.forEach(function(key) {
  Dride.push(key.rideCount);
});
var myObj = {},
  myObj1 = {},
  myObj2 = {},
   monk=['Slot','passenger_data','driver_data'];
  var arr = [];
  for(var i=0;i<3;i++){
myObj[monk[i]] = slot1[i];
myObj1[monk[i]] = Pride[i];
myObj2[monk[i]]  = Dride[i];
}
arr.push(myObj, myObj1, myObj2)
console.log(JSON.stringify(arr))
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

5 个答案:

答案 0 :(得分:4)

假设slotsdriver_datapassenger_data的顺序相同,则可以像这样在mapdriver_data

var obj = {"driver_data":[{"slot":0,"rideCount":98},{"slot":30,"rideCount":75},{"slot":100,"rideCount":0}],"passenger_data":[{"slot":0,"rideCount":33},{"slot":30,"rideCount":56},{"slot":100,"rideCount":37}]}

let output = obj.driver_data.map((d, index) => ({ 
        Slot: slot, 
        driver_data: d.rideCount, 
        passenger_data: obj.passenger_data[index].rideCount
     }))

console.log(output)

如果slots的顺序不同,则可以像这样使用mapfind。使用与驱动程序相同的slot获取乘客。返回具有计数值的新对象

var obj={"driver_data":[{"slot":0,"rideCount":98},{"slot":30,"rideCount":75},{"slot":100,"rideCount":0}],"passenger_data":[{"slot":0,"rideCount":33},{"slot":30,"rideCount":56},{"slot":100,"rideCount":37}]}

let output = obj.driver_data.map(d => {
  let passenger = obj.passenger_data.find(p => p.slot === d.slot);
  return {
    Slot: d.slot,
    driver_data: d.rideCount,
    passenger_data: (passenger || {}).rideCount
  }
})

console.log(output)

或使用Array.from(假设slot正确)

var obj={"driver_data":[{"slot":0,"rideCount":98},{"slot":30,"rideCount":75},{"slot":100,"rideCount":0}],"passenger_data":[{"slot":0,"rideCount":33},{"slot":30,"rideCount":56},{"slot":100,"rideCount":37}]}

let { driver_data, passenger_data } = obj;

const output = Array.from({ length: driver_data.length }, (_, i) => {
  return {
    Slot: driver_data[i].slot,
    driver_data: driver_data[i].rideCount,
    passenger_data: passenger_data[i].rideCount
  }
})

console.log(output)

答案 1 :(得分:3)

您可以使用slot作为密钥的Map并收集所需的属性。

var data = { driver_data: [{ slot: 0, rideCount: 98 }, { slot: 30, rideCount: 75 }, { slot: 100, rideCount: 0 }], passenger_data: [{ slot: 0, rideCount: 33 }, { slot: 30, rideCount: 56 }, { slot: 100, rideCount: 37 }] },
    result = Array.from(['driver_data', 'passenger_data']
        .reduce(
            (m, key) => data[key].reduce(
                (n, { slot, rideCount }) => n.set(slot, Object.assign(
                    { slot },
                    n.get(slot),
                    { [key]: rideCount }
                )),
                m
            ),
            new Map
        )
        .values()
    );

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 2 :(得分:2)

使用find更好地先从这两个数据中的任何一个创建Map或对象,因为在Map或Object上进行搜索具有O(1)复杂性。

var object = {
  "driver_data": [{
    "slot": 0,
    "rideCount":98
  }, {
    "slot": 30,
    "rideCount": 75
  }, {
    "slot": 100,
    "rideCount": 0
  }],
  "passenger_data": [{
    "slot": 0,
    "rideCount":33
  }, {
    "slot": 30,
    "rideCount": 56
  }, {
    "slot": 100,
    "rideCount": 37
  }]
}

let data = object.driver_data.reduce((acc, curr) => {
	acc[curr.slot] = curr.rideCount;
	return acc;
}, {});
let out = object.passenger_data.map(({slot, rideCount}) => ({slot, passenger_data: rideCount, driver_data: data[slot]}));
console.log(out)

答案 3 :(得分:1)

您可以像这样使用map

var object = {"driver_data": [{"slot": 0,"rideCount": 98}, {"slot": 30,"rideCount": 75}, {"slot": 100,"rideCount": 0}],"passenger_data": [{"slot": 0,"rideCount": 33}, {"slot": 30,"rideCount": 56}, {"slot": 100,"rideCount": 37}]};
var newObject = object.driver_data.map(driver => {
  var passenger = object.passenger_data.find(({ slot }) => slot == driver.slot);
  return {
    slot: driver.slot,
    passenger_data: passenger.rideCount,
    driver_data: driver.rideCount
  }
});
console.log(newObject);
.as-console-wrapper { max-height: 100% !important; top: auto; }

答案 4 :(得分:1)

var object = {
  "driver_data": [{
    "slot": 0,
    "rideCount":98
  }, {
    "slot": 30,
    "rideCount": 75
  }, {
    "slot": 100,
    "rideCount": 0
  }],
  "passenger_data": [{
    "slot": 0,
    "rideCount":33
  }, {
    "slot": 30,
    "rideCount": 56
  }, {
    "slot": 100,
    "rideCount": 37
  }]
}

var driver_data = object.driver_data;
var passenger_data = object.passenger_data;
var result = [];
for(var i=0;i<driver_data.length;i++){
    result.push({Slot:driver_data[i].slot,passenger_data:driver_data[i].rideCount,driver_data:passenger_data[i].rideCount});
}
console.log(result);