查找可能的目的地会给出值列表

问题描述 投票:1回答:3

我有以下有效载荷

[ 
  {
    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;
};
javascript
3个回答
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
投票

该列表有三个嵌套在数组中的数据有效负载,数据模式如下:

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

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);
© www.soinside.com 2019 - 2024. All rights reserved.