我有以下有效载荷
[
{
source: 'A',
arrive: null,
depart: 30
},
{
source: 'B',
arrive: 45,
depart: 40
},
{
source: 'C',
arrive: 25,
depart: null
}
]
预期的输出如下
[
{
source: 'A',
dest: 'B'
arrive: 45,
depart: 30
},
{
source: 'A',
dest: 'C',
arrive: 25,
depart: 30
},
{
source: 'B',
dest: 'C',
arrive: 25,
depart: 40
}
]
我正在尝试使用递归来获得预期的输出并在逻辑上挣扎。达到上述预期产出的最佳方法是什么?下面是我现在所拥有的,不确定是否正确的解决方法
我在这里使用排列逻辑,能够将我的对象标记为1,2,3
determineRoutes([{1}, {2}, {3}]) {1} + determineRoutes([{2}, {3}]) ...
const determineRoutes = (routes) => {
let result = [];
if(routes.length === 1){
result.push(routes[0]);
return result;
}
for(let i = 0; i < routes.length; i++){
let current = routes[i];
let remaining = routes.slice(i + 1);
let process = determineRoutes(remaining);
for(let j = 0; j < process.length; j++){
if(current.source !== process[i].source && current.depart !== null){
let obj = {};
obj['source'] = current.source;
obj['dest'] = process[i].source;
obj['depart'] = current.depart;
obj['arrive'] = process[i].arrive;
result.push(Object.assign({}, obj));
}
}
}
return result;
};
答案 0 :(得分:1)
这是使用Array.prototype.reduce()
的基本实现:
const input = [{
source: 'A',
arrive: null,
depart: 30
}, {
source: 'B',
arrive: 45,
depart: 40
}, {
source: 'C',
arrive: 25,
depart: null
}];
const output = input.reduce((a, v1, i, data) => {
for (let j = i + 1; j < data.length; j++) {
const v2 = data[j];
a.push({source: v1.source, dest: v2.source, arrive: v2.arrive, depart: v1.depart});
}
return a;
}, []);
console.log(output);
这将产生您的预期输出:
[
{
"source": "A",
"dest": "B",
"arrive": 45,
"depart": 30
},
{
"source": "A",
"dest": "C",
"arrive": 25,
"depart": 30
},
{
"source": "B",
"dest": "C",
"arrive": 25,
"depart": 40
}
]
答案 1 :(得分:1)
该列表具有嵌套在数组中的三个数据有效载荷,数据模式如下:
source: [unique string id]
arrival: [time||false]
depart: [time||false]
我将使用排除原理,并从不离开的源中消除数据输出。因此,下面的代码循环遍历并确定是否将数据发送给调度程序函数,然后为所有允许到达的有效负载调度数据,然后返回主线程循环并查找下一个离开的有效负载。我真的叫递归,因为主循环本身不会调用,但我相信它可能会起作用。
var data = [
{
source: 'A',
arrive: null,
depart: 30
},
{
source: 'B',
arrive: 45,
depart: 40
},
{
source: 'C',
arrive: 25,
depart: null
}
]
var schemaArray = [];
function scheduler(payloaded, schedule){
// Global function that gets the output
var store = {}
// Check schedule for sources that allow arrivals
for(var i = 0; i < schedule.length; i++) {
// Ensure your data source doesn't match the payload
if(schedule[i].source !== payloaded.source && schedule[i].arrive !== null) {
// store a scheduled payload
store.source = payloaded.source;
store.dest = schedule[i].source;
store.arrive = schedule[i].arrive;
store.depart = payloaded.depart;
// push to the schemaArry
schemaArray.push(store);
}
else { null; }
}
return true;
}
// Loop through the array payload finding sources that have no departures
// To eliminate it from inclusion in the returned output
// Since the expected output only includes sources that depart.
for(var z = 0; z < data.length; z++) {
// Here I use a ternary operation but this
// checking to see if the data payload departs
data[z].depart === null ?
null : scheduler(data[z], data);
}
console.log(schemaArray);
答案 2 :(得分:1)
const nodes = [
{
source: 'A',
arrive: null,
depart: 30
},
{
source: 'B',
arrive: 45,
depart: 40
},
{
source: 'C',
arrive: 25,
depart: null
}
];
const results = nodes.filter(node => node.depart) // Filter the nodes to just the ones that have a depart
.reduce((results, node) => // Reduce the nodes to an array of results
[
...results, // Spread the existing results into a new array
...nodes.filter(n => n.arrive && n.source !== node.source) // Filter the nodes to just the ones that have an arrive and are not the current node
.map(
n => ({ source: node.source, dest: n.source, arrive: n.arrive, depart: node.depart })
) // Combine the source node and destination node into a result object
],
[] // The initial empty array to with no results
);
console.log(results);