0

So I have two arrays like this.

let emailList = [ [ 'OM Email', 'Team/Location' ],
                  [ '[email protected]', 'Addison' ],
                  [ '[email protected]', 'SouthArlington' ]]

let data = [ [ 'Addison',13373,'Addison','Office Team',4,'Jordan M','DFW','General',2,1,0,31,63,
              73 ],
             [ 'SouthArlington',13373,'SouthArlington','Office Team',4,'Jordan M','DFW','General',2,1,0,31,63,
              73 ],

[ 'Addison',13374,'Addison','Office Team3',5,'Jordan M','DFW','General',2,1,0,31,63, 73 ] ]

id like to filter through the data array and see if it finds a location from the emailList. (The position from the data is at 2 ) if it does find a location from the data, pop it and create a new array each time for each location.

What I am trying to do is I am filtering through a google-sheets for when a certain column equals something. That is currently working, thats the information from the data array. What I am trying to do is send a notification to each office from the emailList along with their respectives rows.

this is how iam filtering data from the google-sheet doc

  let data = thisSpreadsheet.filter(function (row,index) {
      return row[11] >= 30 
  });

if i filter again on data, i get the results but i need it for each matching office.

let locationList  = data.filter(function(location){

    return location[2] === emailList[1][1]

})

Expected:

    let Addison = [[ 'Addison',13373,'Addison','Office Team',4,'Jordan M','DFW','General',2,1,0,31,63,
                  73 ],
[ 'Addison',13374,'Addison','Office Team3',5,'Jordan M','DFW','General',2,1,0,31,63,
                  73 ]]

    let SouthArlington = [ 'SouthArlington',13373,'SouthArlington','Office Team',4,'Jordan M','DFW','General',2,1,0,31,63,
                  73 ]

2 Answers 2

2

The filter you need:

let locationList  = data.filter(function(location){
    return emailList.some(office => office[1] === location[2]);
})

Here we use Array.some() and check for every data if its location is contained in any of the offices in the array.

Another option is to create an object:

let result = {};
let locationList  = data.filter(function(location){
    return emailList.some(office => office[1] === location[2]);
})
locationList.forEach(loc => result[loc[2]] = loc);

Then result contains: {Addison: Array(14), SouthArlington: Array(14)}

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you, how can I seperate each office into its own data set? What I am trying to do is to be able to email this array to that specific office email. (programmatically)
Can you specify with an example the exact structure you wish to have in the end?
Yes, sorry about that. I just updated the expected results.
Ok, now I see what you want. The current solution I wrote gives you this, but in an array. So instead of variable let Addison, you have it in the [0] cell of the array. I may have something better than array for you: see the edited answer
Yes, this is almost perfect. how do i tackle multiple values for a certain location? (i updated my data set.
1

You can use Array.prototype.reduce to create a map that stores the data based on location as the map key. See the comments below:

let data = [
  ['Addison', 13373, 'Addison', 'Office Team', 4, 'Jordan M', 'DFW', 'General', 2, 1, 0, 31, 63, 73],
  ['SouthArlington', 13373, 'SouthArlington', 'Office Team ', 4, ' Jordan M ', ' DFW ', ' General ', 2, 1, 0, 31, 63, 73]
];

// Call Array.prototype.reduce on data
const emailByLocation = data.reduce((map, arr) => {
  const [location, ...rest] = arr;

  if (map.get(location) == null) {
    // Add a new map entry each time we encounter a new location name in
    // data. The key of the entry will be the location name (which is in
    // the first element of arr parameter) and set the value to an array 
    // that contains 1 item (i.e.: arr).
    map.set(location, [arr]);
  } else {
    // Add additional to the map entry array value for this location.
    map.get(location).push(arr);
  }

  return map;
}, new Map());

// Iterate over the map entries to see the values
emailByLocation.forEach((value, key) => {
  console.log(`[${key}] = ${value}`);
});

// Since emailByLocation is a Map, you can take advantage of that  
// and get the data for each location simply by passing the key (i.e.: the location name).
console.log('Here is the info for Addison:' + emailByLocation.get("Addison"));
console.log('Here is the info for SouthArlington:' + emailByLocation.get("SouthArlington"));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.