简体   繁体   中英

How do I check if values in one array feature in a separate array, and then if they do, return those values after converting them each to new values

I have an array with ID numbers and a second array with names for those ID numbers. I then have a third array that will only contain a few of the ID numbers. What I want to do is check the values in the third array against the first array that contains all the IDs. Then the IDs that feature in both the first and third array I want to return as the matching names in the second array that has the names for the IDs in the first array.

let seriesIDs = [ 5862, 5869, 5865, 5883, 5884, 5887 ];
let seriesNames = [ '9XM', '9XE', '8XE', '8XT', '8XEP', '2NX' ];
seriesIDs = seriesNames;

let thirdArray = [ 5869, 5865 ]; 

(these values won't be known beforehand. I will have the third array which I will loop through and check which values feature in the array named seriesIDs above)

I need to loop thirdArray and then the values in thirdArray I need to check against the array seriesIDs and the matching values in that array should trigger the return of the corresponding names in the array seriesNames.

so for example if thirdArray contains the values [ 5869, 5865 ] then what I would want returned would be '9XE' and '8XE'

Can anyone help?

What I would do is switch from two arrays to one object. Let's say instead of:

let seriesIDs = [ 5862, 5869, 5865, 5883, 5884, 5887 ];
let seriesNames = [ '9XM', '9XE', '8XE', '8XT', '8XEP', '2NX' ];

I'd do:

const seriesMap = {
    5862: '9XM',
    5869: '9XE',
    ...
    5887: '2NX'
};

Then you could simply check names by indexer operator:

const firstName = seriesMap[thirdArray[0]];
const secondName = seriesMap[thirdArray[1]];

Anyway if such solution is not possible for you. You can do it like this:

let seriesIDs = [ 5862, 5869, 5865, 5883, 5884, 5887 ];
let seriesNames = [ '9XM', '9XE', '8XE', '8XT', '8XEP', '2NX' ];
let thirdArray = [ 5869, 5865 ]; 

const names = [];
for(const id of thirdArray) {
    const idx = seriesIDs.indexOf(id);
    if(idx !== -1) {
        names.push(seriesNames[idx]);
    }
}

console.log(names);

A helpful first step is to create one array of pairs

  • [5862, '9XM']
  • [5869, '9XE']

from the two arrays. You can do that by mapping each element in one array to that pair according to its index, then make a function out of that operation and call it zip :

const zip = (a, b) => a.map((x, i) => [x, b[i]]);

An array of pairs is exactly what the Map constructor wants:

const namesByID = new Map(zip(seriesIDs, seriesNames));

and that Map will let you look up names… by id. (Example: namesByID.get(5887) === '2NX' .)

From there, convert your array of ids to look up into an array of either names (id found in the map) or undefined s (what Map#get returns when the key doesn't exist):

thirdArray.map(id => namesByID.get(id))

and take out the undefined s:

thirdArray
    .map(id => namesByID.get(id))
    .filter(name => name !== undefined)

 const zip = (a, b) => a.map((x, i) => [x, b[i]]); const seriesIDs = [5862, 5869, 5865, 5883, 5884, 5887]; const seriesNames = ['9XM', '9XE', '8XE', '8XT', '8XEP', '2NX']; const namesByID = new Map(zip(seriesIDs, seriesNames)); const thirdArray = [5869, 5865]; console.log( thirdArray .map(id => namesByID.get(id)) .filter(name => name !== undefined) ); 

你可以试试


seriesIDs.map((id, index) => thirdArray.includes(id) && seriesNames[index]).filter(item => item)

Create an object lookup using the seriesIDs and seriesNames , then using array#map get the series name for each series ID.

 let seriesIDs = [ 5862, 5869, 5865, 5883, 5884, 5887 ], seriesNames = [ '9XM', '9XE', '8XE', '8XT', '8XEP', '2NX' ], thirdArray = [ 5869, 5865 ], lookup = Object.assign(...seriesIDs.map((v,i) => ({[v]: seriesNames[i]}))), result = thirdArray.map(v => lookup[v]); console.log(result); 
 .as-console-wrapper {max-height: 100% !important; top: 0;} 

Instead of maintaining 2 arrays better you make array of an object. ie:

let serialIDs=[{id:5862, name:'9XM'},{id:5869, name:'9XE'},{id:5865, name:'8XE'},{id:5883, name:'8XT'},{id:5884, name:'8XEP'},{id:5887 , name:'2NX'}];

Then do filtering:

serialIDs.filter(function(item) {  return thirdArray.includes(item.id); });  // It will return filtered array

Here's a solution

Keep names when the corresponding id is present

 const selectIDs = (seriesIDs, seriesNames, ids) => seriesNames.filter((_, i) => ids.includes(seriesIDs[i])) const seriesIDs = [ 5862, 5869, 5865, 5883, 5884, 5887 ]; const seriesNames = [ '9XM', '9XE', '8XE', '8XT', '8XEP', '2NX' ]; const thirdArray = [ 5869, 5865 ] console.log(selectIDs(seriesIDs, seriesNames, thirdArray)) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM