通过使用第二个数组中的引用从第一个数组中的每个对象中提取每个值来创建一个新数组(JavaScript)

Posted

技术标签:

【中文标题】通过使用第二个数组中的引用从第一个数组中的每个对象中提取每个值来创建一个新数组(JavaScript)【英文标题】:Make a new array by extracting each value from each object in the 1st array using the references in the 2nd array (JavaScript) 【发布时间】:2022-01-05 08:43:26 【问题描述】:

我有 2 个数组

第一个数组4个类别对象每个对象“name”和“ref”(引用)强>:

let categories = [
  
    "name": "Books", 
    "ref": "categories/category1"
  ,
  
    "name": "Computers", 
    "ref": "categories/category2"
  ,
  
    "name": "Drink", 
    "ref": "categories/category3"
  ,
  
    "name": "Food", 
    "ref": "categories/category4"
  
];

第二个数组对类别的引用

let refs = ["categories/category2", "categories/category4"];

现在,我想通过从第一个数组“类别”变量中提取仅类别名称来制作类别名称的新数组 第二个数组“refs”变量中的引用

我使用 2 个数组创建了代码来制作新的类别名称数组,并且它完美地工作制作了新数组其中有 2 个类别名称“计算机”和“食品”

let newArr = [];

for(let i = 0; i < refs.length; i++) 
  for(let j = 0; j < categories.length; j++) 
    if(refs[i] == categories[j].ref) 
      newArr.push(categories[j].name);
      
  


console.log(newArr); // ["Computer", "Food"]

这是完整的可运行代码

let categories = [
  
    "name": "Book", 
    "ref": "categories/category1"
  ,
  
    "name": "Computer", 
    "ref": "categories/category2"
  ,
  
    "name": "Shoes", 
    "ref": "categories/category3"
  ,
  
    "name": "Food", 
    "ref": "categories/category4"
  
];

let refs = ["categories/category2", "categories/category4"];

let newArr = [];

for(let i = 0; i < refs.length; i++) 
  for(let j = 0; j < categories.length; j++) 
    if(refs[i] == categories[j].ref) 
      newArr.push(categories[j].name);
      
  


console.log(newArr); // ["Computer", "Food"]

但是,我想让这段代码更简单。有什么方法可以让这段代码更简单

let newArr = [];

for(let i = 0; i < refs.length; i++) 
  for(let j = 0; j < categories.length; j++) 
    if(refs[i] == categories[j].ref) 
      newArr.push(categories[j].name);
      
  


console.log(newArr); // ["Computer", "Food"]

【问题讨论】:

【参考方案1】:

您可以在外部for语句中使用"find()""includes()"来简化您的代码:

let categories = [
   "name": "Book", "ref": "categories/category1" ,
   "name": "Computer", "ref": "categories/category2" ,
   "name": "Shoes", "ref": "categories/category3" ,
   "name": "Food", "ref": "categories/category4"
];

let refs = ["categories/category2", "categories/category4"];

let newArr = [];

for(let i = 0; i < refs.length; i++) 
  newArr.push(categories.find(v => v.ref.includes(refs[i])).name);


console.log(newArr); // ["Computer", "Food"]

【讨论】:

【参考方案2】:

您可以使用 "map()""find()" 来简化您的代码:

let categories = [
   "name": "Book", "ref": "categories/category1" ,
   "name": "Computer", "ref": "categories/category2" ,
   "name": "Shoes", "ref": "categories/category3" ,
   "name": "Food", "ref": "categories/category4"
];

let refs = ["categories/category2", "categories/category4"];

let newArr = [];

newArr = refs.map(v1 => categories.find(v2 => v2.ref == v1).name);

console.log(newArr); // ["Computer", "Food"]

【讨论】:

【参考方案3】:

使用filter 仅获取 ref 在 refs 内的类别。

如果你只想要名字,你可以使用map。

let categories = [
   "name": "Book", "ref": "categories/category1" ,
   "name": "Computer", "ref": "categories/category2" ,
   "name": "Shoes", "ref": "categories/category3" ,
   "name": "Food", "ref": "categories/category4" 
];

let refs = ["categories/category2", "categories/category4"];

let filtered = categories.filter((c) => refs.includes(c.ref));
console.log(filtered);
// [name: "Computer", ref: "categories/category2", name: "Food", ref: "categories/category4"]

let onlyNames = filtered.map((c) => c.name);
console.log(onlyNames);
// ["Computer", "Food"];

【讨论】:

以上是关于通过使用第二个数组中的引用从第一个数组中的每个对象中提取每个值来创建一个新数组(JavaScript)的主要内容,如果未能解决你的问题,请参考以下文章

如果找到第一个数组中的特定键,则在第二个数组中查找值

交错两个 numpy 索引数组,每个数组中的一项

比较两个数组并查找第二个数组中缺少的项目[重复]

如何一次循环遍历 2 个数组并将数组中的颜色分配给另一个数组的每个值

循环遍历python数组以匹配第二个数组中的多个条件,快速方法?

通过索引从数组中获取对象,即从另一个数组中获取