遍历对象数组以搜索起点 -> 目的地和目的地 -> 起点和拉取计数
loop through an array of objects to search for origin -> destination and destination -> origin and pull counts
我有一组包含起点和终点信息的对象,显示了从一个地方到另一个地方的对象总量。
我正在尝试比较两个城市之间的流量。比如下图,当起点是温哥华,终点是旧金山时,计数是5,反之(起点是旧金山,终点是温哥华),计数是12。
InOut = [
{
"origin": "Pittsburgh, Pennsylvania",
"dest": "New York, New York",
"count": 5
},
{
"origin": "Pittsburgh, Pennsylvania",
"dest": "Newark, New Jersey",
"count": 2
},
{
"origin": "Los Angeles, California",
"dest": "Seattle, Washington",
"count": 6
},
{
"origin": "Vancouver, Canada",
"dest": "Brooklyn, New York",
"count": 3
},
{
"origin": "Detroit, Michigan",
"dest": "New York, New York",
"count": 4
},
{
"origin": "Detroit, Michigan",
"dest": "Washington, DC",
"count": 11
},
{
"origin": "Vancouver, Canada",
"dest": "San Francisco, California",
"count": 5
},
{
"origin": "New York, New York",
"dest": "Pittsburgh, Pennsylvania",
"count": 9
},
{
"origin": "New York, New York",
"dest": "Detroit, Michigan",
"count": 7
},
{
"origin": "Philadelphia, Pennsylvania",
"dest": "Baltimore, Maryland",
"count": 12
},
{
"origin": "Philadelphia, Pennsylvania",
"dest": "New York, New York",
"count": 6
},
{
"origin": "Seattle, Washington",
"dest": "Los Angeles, California",
"count": 3
},
{
"origin": "San Francisco, California",
"dest": "Vancouver, Canada",
"count": 12
}
]
我知道我可以像这样根据出发地和目的地查找计数:
function findValueByKey(array, key, value, key2, value2) {
for (var i = 0; i < array.length; i++) {
if (array[i][key] == value && array[i][key2] == value2) {
return array[i].count;
}
}
return null;
}
var obj = findValueByKey(InOut, 'origin', 'Vancouver, Canada', 'dest', 'San Francisco, California');
var obj2 = findValueByKey(InOut, 'origin', 'San Francisco, California', 'dest', 'Vancouver, Canada');
console.log('obj', obj)
console.log('obj', obj2)
但是,我很难弄清楚如何遍历数组以获取所有计数。这是我到目前为止的情况:
allLocations = ["Pittsburgh, Pennsylvania", "Los Angeles, California", "Newark, New Jersey", "Seattle, Washington", "Vancouver, Canada", "Brooklyn, New York", "Detroit, Michigan", "Washington, DC", "New York, New York", "Philadelphia, Pennsylvania", "San Francisco, California"]
origDestValues = []
for(i=0; i< allLocations.length;i++){
InOutA = findValueByKey(setOne, 'origin', allLocations[i], 'dest', allLocations[i])
InOutB = findValueByKey(setOne, 'origin', allLocations[i], 'dest', allLocations[i])
origDestValues.push({
city1: allLocations[i],
city2: allLocations[i],
firstCount: InOutA,
secondCount: InOutB
});
};
显然这是行不通的,因为它每次都会寻找相同的 origin/destination,但我认为这是朝着正确的方向前进?我想证明我已经付出了一些努力,然后再问这里。
此外,就其价值而言,我的实际数组要长得多(更多城市)。为了这个问题,我已经简化了它。
感谢您any/all的帮助。
您可以运行此代码查看输出:
const inOut = [{
"origin": "Pittsburgh, Pennsylvania",
"dest": "New York, New York",
"count": 5
},
{
"origin": "Pittsburgh, Pennsylvania",
"dest": "Newark, New Jersey",
"count": 2
},
{
"origin": "Los Angeles, California",
"dest": "Seattle, Washington",
"count": 6
},
{
"origin": "Vancouver, Canada",
"dest": "Brooklyn, New York",
"count": 3
},
{
"origin": "Detroit, Michigan",
"dest": "New York, New York",
"count": 4
},
{
"origin": "Detroit, Michigan",
"dest": "Washington, DC",
"count": 11
},
{
"origin": "Vancouver, Canada",
"dest": "San Francisco, California",
"count": 5
},
{
"origin": "New York, New York",
"dest": "Pittsburgh, Pennsylvania",
"count": 9
},
{
"origin": "New York, New York",
"dest": "Detroit, Michigan",
"count": 7
},
{
"origin": "Philadelphia, Pennsylvania",
"dest": "Baltimore, Maryland",
"count": 12
},
{
"origin": "Philadelphia, Pennsylvania",
"dest": "New York, New York",
"count": 6
},
{
"origin": "Seattle, Washington",
"dest": "Los Angeles, California",
"count": 3
},
{
"origin": "San Francisco, California",
"dest": "Vancouver, Canada",
"count": 12
}
]
// Step 1. Find all possible cities:
const cities = {}
// because we are using an object, we won't have any duplicates
for (const unit of inOut) {
cities[unit.origin] = {}
cities[unit.dest] = {}
}
// Step 2. Generate a matrix of all cities to all cities:
for (const city1 in cities) {
for (const city2 in cities) {
if (city1 !== city2) { // we don't need a dest == origin pair
cities[city1][city2] = {
to: 0,
from: 0
}
}
}
}
// Step 3. Populate the matrix twice: once for a destination and once for an origin:
for (const io of inOut) {
cities[io.origin][io.dest].to += io.count
cities[io.dest][io.origin].from += io.count
}
// Step 4. Optionally, we can remove empty pairs, where to and from are 0
for (const city1 in cities) {
for (const city2 in cities[city1]) {
if (cities[city1][city2].to === 0 && cities[city1][city2].from === 0) {
delete cities[city1][city2]
}
}
}
console.log("List of all combinations:")
console.log(cities)
console.log("For example, info about one city (Pittsburgh, Pennsylvania) can be found like that:")
console.log(cities["Pittsburgh, Pennsylvania"])
console.log("For example, the flow between Pittsburgh, Pennsylvania and New York, New York can be found like that:")
console.log(cities["Pittsburgh, Pennsylvania"]["New York, New York"])
console.log("You can also chech the opposite direction (the flow between and New York, New York and Pittsburgh, Pennsylvania):")
console.log(cities["New York, New York"]["Pittsburgh, Pennsylvania"])
console.log("Note that for a non-existant pair (there were no transactions), you'll get undefined:")
console.log(cities["Pittsburgh, Pennsylvania"]["Vancouver, Canada"])
我有一组包含起点和终点信息的对象,显示了从一个地方到另一个地方的对象总量。 我正在尝试比较两个城市之间的流量。比如下图,当起点是温哥华,终点是旧金山时,计数是5,反之(起点是旧金山,终点是温哥华),计数是12。
InOut = [
{
"origin": "Pittsburgh, Pennsylvania",
"dest": "New York, New York",
"count": 5
},
{
"origin": "Pittsburgh, Pennsylvania",
"dest": "Newark, New Jersey",
"count": 2
},
{
"origin": "Los Angeles, California",
"dest": "Seattle, Washington",
"count": 6
},
{
"origin": "Vancouver, Canada",
"dest": "Brooklyn, New York",
"count": 3
},
{
"origin": "Detroit, Michigan",
"dest": "New York, New York",
"count": 4
},
{
"origin": "Detroit, Michigan",
"dest": "Washington, DC",
"count": 11
},
{
"origin": "Vancouver, Canada",
"dest": "San Francisco, California",
"count": 5
},
{
"origin": "New York, New York",
"dest": "Pittsburgh, Pennsylvania",
"count": 9
},
{
"origin": "New York, New York",
"dest": "Detroit, Michigan",
"count": 7
},
{
"origin": "Philadelphia, Pennsylvania",
"dest": "Baltimore, Maryland",
"count": 12
},
{
"origin": "Philadelphia, Pennsylvania",
"dest": "New York, New York",
"count": 6
},
{
"origin": "Seattle, Washington",
"dest": "Los Angeles, California",
"count": 3
},
{
"origin": "San Francisco, California",
"dest": "Vancouver, Canada",
"count": 12
}
]
我知道我可以像这样根据出发地和目的地查找计数:
function findValueByKey(array, key, value, key2, value2) {
for (var i = 0; i < array.length; i++) {
if (array[i][key] == value && array[i][key2] == value2) {
return array[i].count;
}
}
return null;
}
var obj = findValueByKey(InOut, 'origin', 'Vancouver, Canada', 'dest', 'San Francisco, California');
var obj2 = findValueByKey(InOut, 'origin', 'San Francisco, California', 'dest', 'Vancouver, Canada');
console.log('obj', obj)
console.log('obj', obj2)
但是,我很难弄清楚如何遍历数组以获取所有计数。这是我到目前为止的情况:
allLocations = ["Pittsburgh, Pennsylvania", "Los Angeles, California", "Newark, New Jersey", "Seattle, Washington", "Vancouver, Canada", "Brooklyn, New York", "Detroit, Michigan", "Washington, DC", "New York, New York", "Philadelphia, Pennsylvania", "San Francisco, California"]
origDestValues = []
for(i=0; i< allLocations.length;i++){
InOutA = findValueByKey(setOne, 'origin', allLocations[i], 'dest', allLocations[i])
InOutB = findValueByKey(setOne, 'origin', allLocations[i], 'dest', allLocations[i])
origDestValues.push({
city1: allLocations[i],
city2: allLocations[i],
firstCount: InOutA,
secondCount: InOutB
});
};
显然这是行不通的,因为它每次都会寻找相同的 origin/destination,但我认为这是朝着正确的方向前进?我想证明我已经付出了一些努力,然后再问这里。
此外,就其价值而言,我的实际数组要长得多(更多城市)。为了这个问题,我已经简化了它。
感谢您any/all的帮助。
您可以运行此代码查看输出:
const inOut = [{
"origin": "Pittsburgh, Pennsylvania",
"dest": "New York, New York",
"count": 5
},
{
"origin": "Pittsburgh, Pennsylvania",
"dest": "Newark, New Jersey",
"count": 2
},
{
"origin": "Los Angeles, California",
"dest": "Seattle, Washington",
"count": 6
},
{
"origin": "Vancouver, Canada",
"dest": "Brooklyn, New York",
"count": 3
},
{
"origin": "Detroit, Michigan",
"dest": "New York, New York",
"count": 4
},
{
"origin": "Detroit, Michigan",
"dest": "Washington, DC",
"count": 11
},
{
"origin": "Vancouver, Canada",
"dest": "San Francisco, California",
"count": 5
},
{
"origin": "New York, New York",
"dest": "Pittsburgh, Pennsylvania",
"count": 9
},
{
"origin": "New York, New York",
"dest": "Detroit, Michigan",
"count": 7
},
{
"origin": "Philadelphia, Pennsylvania",
"dest": "Baltimore, Maryland",
"count": 12
},
{
"origin": "Philadelphia, Pennsylvania",
"dest": "New York, New York",
"count": 6
},
{
"origin": "Seattle, Washington",
"dest": "Los Angeles, California",
"count": 3
},
{
"origin": "San Francisco, California",
"dest": "Vancouver, Canada",
"count": 12
}
]
// Step 1. Find all possible cities:
const cities = {}
// because we are using an object, we won't have any duplicates
for (const unit of inOut) {
cities[unit.origin] = {}
cities[unit.dest] = {}
}
// Step 2. Generate a matrix of all cities to all cities:
for (const city1 in cities) {
for (const city2 in cities) {
if (city1 !== city2) { // we don't need a dest == origin pair
cities[city1][city2] = {
to: 0,
from: 0
}
}
}
}
// Step 3. Populate the matrix twice: once for a destination and once for an origin:
for (const io of inOut) {
cities[io.origin][io.dest].to += io.count
cities[io.dest][io.origin].from += io.count
}
// Step 4. Optionally, we can remove empty pairs, where to and from are 0
for (const city1 in cities) {
for (const city2 in cities[city1]) {
if (cities[city1][city2].to === 0 && cities[city1][city2].from === 0) {
delete cities[city1][city2]
}
}
}
console.log("List of all combinations:")
console.log(cities)
console.log("For example, info about one city (Pittsburgh, Pennsylvania) can be found like that:")
console.log(cities["Pittsburgh, Pennsylvania"])
console.log("For example, the flow between Pittsburgh, Pennsylvania and New York, New York can be found like that:")
console.log(cities["Pittsburgh, Pennsylvania"]["New York, New York"])
console.log("You can also chech the opposite direction (the flow between and New York, New York and Pittsburgh, Pennsylvania):")
console.log(cities["New York, New York"]["Pittsburgh, Pennsylvania"])
console.log("Note that for a non-existant pair (there were no transactions), you'll get undefined:")
console.log(cities["Pittsburgh, Pennsylvania"]["Vancouver, Canada"])