如何用一个公共变量对两个不同的数组进行分组?

时间:2022-05-01 09:02:34

I have two different arrays. one will be for the title of the national park and other data I can use from that, and the other is alerts from the whole NPS system. I have compared the two arrays and the one distinct thing they have in common is parkCode. How can I go about grouping them by national park...example being:

我有两个不同的数组。一个是国家公园的标题和我可以使用的其他数据,另一个是来自整个NPS系统的警报。我比较了两个数组,它们共同的一个不同之处是parkCode。我如何去国家公园分组......例如:

  1. Yellowstone National Park -alert from yellowstone
  2. 黄石国家公园 - 黄石公园

  3. Arches National Park -alert from arches
  4. 拱门国家公园 - 拱门

here is the data I am working with..

这是我正在使用的数据..

National Park Names:

国家公园名称:

 0:
 description: "."
 designation: "National Park"
 directionsInfo: "From Boston take I-95 north to Augusta, Maine, then 
 Route 3 east to Ellsworth, and on to Mount Desert Island. For an 
 alternate route, continue on I-95 north to Bangor, Maine, then take 
 Route 1A east to Ellsworth. In Ellsworth, take Route 3 to Mount Desert 
 Island."
 directionsUrl: "http://www.nps.gov/acad/planyourvisit/directions.htm"
 fullName: "Acadia National Park"
 id: "6DA17C86-088E-4B4D-B862-7C1BD5CF236B"
 latLong: "lat:44.30777545, long:-68.30063316"
 name: "Acadia"
 **parkCode: "acad"**
 states: "ME"
 url: "https://www.nps.gov/acad/index.htm"

Example Alert:

113:
category: "Park Closure"
description: "The Elwha area is closed indefinitely to vehicle traffic 
beyond Madison Falls parking lot due to extensive flood damage to the 
Olympic Hot Springs Road. There is limited parking and turnaround space 
at the Madison Falls parking area."
id: "84646EA9-1DD8-B71B-0BD7AECDC56BD8AE"
**parkCode: "acad"**
title: "Elwha (Olympic Hot Springs) Road Closed to Vehicle Access"
url: "https://www.nps.gov/olym/planyourvisit/current-road-conditions.htm"

2 个解决方案

#1


0  

Assuming you have your alerts as JavaScript objects in an array called alerts, you can "group" them by park in an object like this:

假设您将警报作为JavaScript对象放入名为alert的数组中,您可以将它们“停放”在一个对象中,如下所示:

const alertsByPark = {};
alerts.forEach(alert => {
  // If there are no alerts recorded for the park yet, add the park as a key to the alertsByPark object
  if (!alertsByPark[alert.parkCode]) alertsByPark[alert.parkCode] = [];
  alertsByPark[alert.parkCode].push(alert);
});

this will set alertsByPark to a data format that looks like this:

这会将alertsByPark设置为如下所示的数据格式:

{
  acad: [...],
  ...
}

with a key for each parkCode and an array of all of the alerts from that park.

每个parkCode的密钥和来自该公园的所有警报的数组。

To output data in the format you show in your question, where each alert is shown after the formal name of the park referenced in its parkCode\, you can use find on the array of parks to get the full park data for the parkCode referenced in the alert.

要以您在问题中显示的格式输出数据,其中每个警报显示在parkCode \中引用的公园的正式名称之后,您可以使用公园阵列上的find来获取parkCode中引用的完整公园数据。警报。

alerts.forEach((alert, i) => {
  // Locate the park with the parkCode matching the alert parkCode
  const park = parks.find(({ parkCode }) => parkCode === alert.parkCode);
  console.log(`${i}. ${park.fullName} - ${alert.description}`);
})

This again assumes you have your alerts in an array of objects called alerts and that you have your parks in an array of objects called parks. It will output a list like

这再次假设您在一组称为警报的对象中发出警报,并且您将公园放在一个名为park的对象数组中。它会输出一个列表

1. Acadia National Park - The Elwha area is closed indefinitely to...
2. park name - alert description

etc

#2


0  

parks.map(park => {
 park.alerts = alerts.filter(alert => alert.parkCode === park.parkCode)
 return park
})

This returns a parks array with prop 'alerts' (array of alerts)

这将返回带有'警报'(警报数组)的公园数组

Example:

const parks = [
 { name: 'Yellowstone', parkCode: 'ys' },
 { name: 'Arcadia', parkCode: 'ar' },
 { name: 'Arches', parkCode: 'arch' },
 { name: 'A park without alerts', parkCode: 'PARK' }
 ]

const alerts = [
 { description: 'Alert 1', parkCode: 'ys' },
 { description: 'Alert 2', parkCode: 'ys' },
 { description: 'Alert 3 ', parkCode: 'ys' },
 { description: 'Alert 4', parkCode: 'ys' },
 { description: 'Alert 5', parkCode: 'arch' },
 { description: 'Alert 6', parkCode: 'ar' },
 { description: 'Alert 7', parkCode: 'ys' },
 { description: 'Alert 8', parkCode: 'arch' },
 { description: 'Alert 9', parkCode: 'ys' },
 { description: 'Alert 10', parkCode: 'ys' },
 { description: 'Alert 11', parkCode: 'ar' },
 { description: 'Alert 12', parkCode: 'ys' },
 { description: 'Alert 13', parkCode: 'ar' },
 { description: 'Alert 14', parkCode: 'ar' },
 { description: 'Alert 15', parkCode: 'ys' },
 { description: 'An alert to unknown park', parkCode: 'ALERT' }
 ]

 let parksWithAlerts = parks.map(park => {
   park.alerts = alerts.filter(alert => alert.parkCode === park.parkCode)
   return park
 })

 console.dir(parksWithAlerts[0])

{ name: 'Yellowstone',
  parkCode: 'ys',
  alerts:
   [ { description: 'Alert 1', parkCode: 'ys' },
   { description: 'Alert 2', parkCode: 'ys' },
   { description: 'Alert 3 ', parkCode: 'ys' },
   { description: 'Alert 4', parkCode: 'ys' },
   { description: 'Alert 7', parkCode: 'ys' },
   { description: 'Alert 9', parkCode: 'ys' },
   { description: 'Alert 10', parkCode: 'ys' },
   { description: 'Alert 12', parkCode: 'ys' },
   { description: 'Alert 15', parkCode: 'ys' } ] }

Joined alerts

let parksWithAlerts = parks.map(park => {
   park.alerts = alerts.filter(alert => alert.parkCode === park.parkCode)
   return park
 }).map(park =>{
    park.alerts = park.alerts.map(alert => alert.description).join(' - ')
    return park
 })



console.dir(parksWithAlerts[0])

{ name: 'Yellowstone',
  parkCode: 'ys',
  alerts: 'Alert 1 - Alert 2 - Alert 3  - Alert 4 - Alert 7 - Alert 9 - Alert 10 - Alert 12 - Alert 15' }

#1


0  

Assuming you have your alerts as JavaScript objects in an array called alerts, you can "group" them by park in an object like this:

假设您将警报作为JavaScript对象放入名为alert的数组中,您可以将它们“停放”在一个对象中,如下所示:

const alertsByPark = {};
alerts.forEach(alert => {
  // If there are no alerts recorded for the park yet, add the park as a key to the alertsByPark object
  if (!alertsByPark[alert.parkCode]) alertsByPark[alert.parkCode] = [];
  alertsByPark[alert.parkCode].push(alert);
});

this will set alertsByPark to a data format that looks like this:

这会将alertsByPark设置为如下所示的数据格式:

{
  acad: [...],
  ...
}

with a key for each parkCode and an array of all of the alerts from that park.

每个parkCode的密钥和来自该公园的所有警报的数组。

To output data in the format you show in your question, where each alert is shown after the formal name of the park referenced in its parkCode\, you can use find on the array of parks to get the full park data for the parkCode referenced in the alert.

要以您在问题中显示的格式输出数据,其中每个警报显示在parkCode \中引用的公园的正式名称之后,您可以使用公园阵列上的find来获取parkCode中引用的完整公园数据。警报。

alerts.forEach((alert, i) => {
  // Locate the park with the parkCode matching the alert parkCode
  const park = parks.find(({ parkCode }) => parkCode === alert.parkCode);
  console.log(`${i}. ${park.fullName} - ${alert.description}`);
})

This again assumes you have your alerts in an array of objects called alerts and that you have your parks in an array of objects called parks. It will output a list like

这再次假设您在一组称为警报的对象中发出警报,并且您将公园放在一个名为park的对象数组中。它会输出一个列表

1. Acadia National Park - The Elwha area is closed indefinitely to...
2. park name - alert description

etc

#2


0  

parks.map(park => {
 park.alerts = alerts.filter(alert => alert.parkCode === park.parkCode)
 return park
})

This returns a parks array with prop 'alerts' (array of alerts)

这将返回带有'警报'(警报数组)的公园数组

Example:

const parks = [
 { name: 'Yellowstone', parkCode: 'ys' },
 { name: 'Arcadia', parkCode: 'ar' },
 { name: 'Arches', parkCode: 'arch' },
 { name: 'A park without alerts', parkCode: 'PARK' }
 ]

const alerts = [
 { description: 'Alert 1', parkCode: 'ys' },
 { description: 'Alert 2', parkCode: 'ys' },
 { description: 'Alert 3 ', parkCode: 'ys' },
 { description: 'Alert 4', parkCode: 'ys' },
 { description: 'Alert 5', parkCode: 'arch' },
 { description: 'Alert 6', parkCode: 'ar' },
 { description: 'Alert 7', parkCode: 'ys' },
 { description: 'Alert 8', parkCode: 'arch' },
 { description: 'Alert 9', parkCode: 'ys' },
 { description: 'Alert 10', parkCode: 'ys' },
 { description: 'Alert 11', parkCode: 'ar' },
 { description: 'Alert 12', parkCode: 'ys' },
 { description: 'Alert 13', parkCode: 'ar' },
 { description: 'Alert 14', parkCode: 'ar' },
 { description: 'Alert 15', parkCode: 'ys' },
 { description: 'An alert to unknown park', parkCode: 'ALERT' }
 ]

 let parksWithAlerts = parks.map(park => {
   park.alerts = alerts.filter(alert => alert.parkCode === park.parkCode)
   return park
 })

 console.dir(parksWithAlerts[0])

{ name: 'Yellowstone',
  parkCode: 'ys',
  alerts:
   [ { description: 'Alert 1', parkCode: 'ys' },
   { description: 'Alert 2', parkCode: 'ys' },
   { description: 'Alert 3 ', parkCode: 'ys' },
   { description: 'Alert 4', parkCode: 'ys' },
   { description: 'Alert 7', parkCode: 'ys' },
   { description: 'Alert 9', parkCode: 'ys' },
   { description: 'Alert 10', parkCode: 'ys' },
   { description: 'Alert 12', parkCode: 'ys' },
   { description: 'Alert 15', parkCode: 'ys' } ] }

Joined alerts

let parksWithAlerts = parks.map(park => {
   park.alerts = alerts.filter(alert => alert.parkCode === park.parkCode)
   return park
 }).map(park =>{
    park.alerts = park.alerts.map(alert => alert.description).join(' - ')
    return park
 })



console.dir(parksWithAlerts[0])

{ name: 'Yellowstone',
  parkCode: 'ys',
  alerts: 'Alert 1 - Alert 2 - Alert 3  - Alert 4 - Alert 7 - Alert 9 - Alert 10 - Alert 12 - Alert 15' }