简体   繁体   English

如何检查一个数组中的值是否在单独的数组中,然后将其转换为新值后返回这些值

[英]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. 我有一个ID编号的数组,第二个数组带有这些ID编号的名称。 I then have a third array that will only contain a few of the ID numbers. 然后,我有了第三个数组,该数组将仅包含一些ID号。 What I want to do is check the values in the third array against the first array that contains all the IDs. 我想要做的是对照包含所有ID的第一个数组检查第三个数组中的值。 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. 然后,我想返回的第一个数组和第三个数组中的ID作为第二个数组中的匹配名称返回,第二个数组中的ID具有第一个数组中ID的名称。

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) (这些值不会事先知道。我将遍历第三个数组,并检查上面名为seriesIDs的数组中的哪些值)

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. 我需要循环thirdArray,然后再对thirdArray中的值进行检查,以检查数组seriesID,并且该数组中的匹配值应触发数组seriesNames中对应名称的返回。

so for example if thirdArray contains the values [ 5869, 5865 ] then what I would want returned would be '9XE' and '8XE' 因此,例如,如果thirdArray包含值[5869、5865],那么我想要返回的将是“ 9XE”和“ 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 : 您可以通过根据一个数组的索引将一个数组中的每个元素映射到该对,然后从该操作中创建一个函数并将其称为zip

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

An array of pairs is exactly what the Map constructor wants: 成对数组正是Map构造函数想要的:

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

and that Map will let you look up names… by id. Map将允许您通过ID查找姓名。 (Example: namesByID.get(5887) === '2NX' .) (例如: 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): 从那里,将您的ID数组转换为一个名称(在地图中找到的ID)或undefined s(当键不存在时Map#get返回的内容)的数组:

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

and take out the undefined s: 并取出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. 使用seriesIDsseriesNames创建一个对象查找,然后使用array#map获得每个系列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. 而不是更好地维护2个数组,而是创建一个对象数组。 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 存在相应ID时保留名称

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将.each调用的返回值保存到数组中 - how do i save return values from a .each call into an array 我如何从表的每一行获取值并将其存储在js数组中 - How do i get the values from each row of a table and store them in a js array 如何在不更改接收新值的数组的 memory 地址的情况下将一个数组中的值加载到另一个数组中? - How do I load the values from one array, into another array, without changing the memory address of the array receiving the new values? 如何从一个数组和一个对象中.map()一个新的值数组? - How do I .map() a new array of values from an array and an object? 识别数组中的唯一值,每个值都将返回 object - 如何处理循环或 hash map - Identify unique values in the array and for each one will return an object - how to do with a loop or a hash map 如何按另一个数组中的相应值对一个数组进行排序? - How do I sort one array by the corresponding values in another array? 如何使用输入检查数组中的值或部分? - How do I check for values or partical within an array using an input? 如何检查特定数组的值并增加或减少它? - How do I check values of a particular array and increment or decrements it? 如何检查数组是否包含对象的值 - How do i check that an array contains values of an object 在reactjs中,如何映射数组并添加到返回值? - In reactjs, how do I map an array and add   to the return values?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM