简体   繁体   English

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

[英]Make a new array by extracting each value from each object in the 1st array using the references in the 2nd array (JavaScript)

I have 2 arrays .我有2 个 arrays

The 1st array has 4 category objects and each object has "name" and "ref"(reference) :第一个数组4 个类别对象每个 object都有“名称”和“参考”(参考)

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

The 2nd array has the references to categories :第二个数组引用了 categories

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

Now, I want to make the new array of category names by extracting only category names from the 1st array "categories" variable using the references in the 2nd array "refs" variable .现在,我想通过使用第二个数组“refs”变量中的引用第一个数组“类别”变量仅提取类别名称来创建新的类别名称数组。

I created the code to make the new array of category names using 2 arrays and it works perfectly making the new array which has 2 category names "Computer" and "Food" :我创建了代码以使用2 arrays创建新的类别名称数组,它完美地制作了具有 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"]

This is the full runnable code :这是完整的可运行代码

 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"]

However, I want to make this code simpler .但是,我想让这段代码更简单 Are there any ways to make this code simpler ?有没有办法让这段代码更简单

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"]
  • Using Array#reduce , iterate over categories while updating a Map where the key is the ref and the value is the name.使用Array#reduce ,在更新Map时迭代categories ,其中key是引用, value是名称。
  • Using Array#map , iterate over refs and use the above Map to get the corresponding names.使用Array#map ,遍历refs并使用上面的 Map 来获取相应的名称。

 const categories = [ { "name": "Books", "ref": "categories/category1" }, { "name": "Computers", "ref": "categories/category2" }, { "name": "Drink", "ref": "categories/category3" }, { "name": "Food", "ref": "categories/category4" } ], refs = ["categories/category2", "categories/category4"]; const categoryRefNameMap = categories.reduce((map, { name, ref }) => map.set(ref, name), new Map); const newArr = refs.map(ref => categoryRefNameMap.get(ref)); console.log(newArr);

Use filter to get only the categories whose ref is inside refs.使用过滤器仅获取 ref 在 refs 内的类别。

if you only want the name you can use map .如果您只想要名称,则可以使用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"];

You can use "find()" function and "includes()" function in the outer for statement to simplify your code:您可以在外部 for 语句中使用“find()”function“includes()”function来简化代码:

 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"]

You can use "map()" function and "find" function to simplify your code:您可以使用“map()”function“find”function来简化代码:

 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"]

暂无
暂无

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

相关问题 有没有办法使用第一个数组作为 Javascript 中的参考在第二个数组中获取相应的索引值? - Is there a way to get corresponding index value in a 2nd array using the 1st array as a reference in Javascript? 如何从第二个中的每个 object 复制技能属性,并根据属性 id 为第一个数组中的每个 object 设置该技能属性? (JS) - How to copy skill property from each object in the 2nd and set that skill property for each object in the 1st array based on property id? (JS) 如果第二个数组匹配,则对第一个数组中的数字求和 - Summing numbers from 1st array if 2nd array match 如何通过使用Javascript或JQuery对第一个数组进行排序来对第二个数组进行排序? - How to sort 2nd array by sorting the 1st one using Javascript or JQuery? 检查数组中的匹配键,如果找到,则取第二个数组条目,如果在 javascript 中不取第一个数组条目 - Check for matching key in array, if found take 2nd array entries if not take 1st array entries in javascript 第一个单词第一个字符第二个单词第二个字符直到数组结尾 - 1st word 1st character 2nd word 2nd character and on till the end of array 在第一个数组元素中的第二个数组元素中查找字母 - Finding letters in 2nd array element in 1st array element 如何将数据从第二数组添加到第一数组以在Google Visualization API中显示图表 - how to add data from 2nd array to 1st array for showing chart in google Visualization api 比较第二个字符串字符与数组中的第一个字符串 - Compare the 2nd strings characters with the 1st string in an array 我正在尝试在 javascript 中列出数组的所有 NESTED 值,但只获取数组中每个值的第一个值 - I'm trying to list all NESTED values of an array in javascript, but get only the 1st value of each in the array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM