简体   繁体   English

如何使用 JavaScript 从具有相同名称但具有不同类型的数组中获取一个字符串/值?

[英]how to get one string / value from an Array which has the same name but has a different type using JavaScript?

example:例子:

  let before = [ "apple.jpg", "apple.png", "apple.svg" , "banana.png", "banana.jpg" ]

what I want:我想要的是:

 let after = ["apple.jpg", "banana.png"] 

You can create a map which is having file names as keys and files with the same name as values您可以创建一个 map,它的文件名作为键,文件与值同名

let before = [ "apple.jpg", "apple.png", "apple.svg" , "banana.png", "banana.jpg" ]

const fileMap = {};

before.forEach(file => {
    const fileName = file.split('.')[0];
    if (fileMap[fileName]) fileMap[fileName].push(file);
    else fileMap[fileName] = [file];
});

After this step you will get an object (fileMap) like this在这一步之后,你会得到一个像这样的 object (fileMap)

{
    apple: ["apple.jpg", "apple.png", "apple.svg"]
    banana:  ["banana.png", "banana.jpg"]
}

Then you can iterate over this object and get the desired result然后你可以遍历这个 object 并得到想要的结果

const result = [];
Object.keys(fileMap).forEach(key => {
    if (fileMap[key].length > 1)
        result.push(fileMap[key][0]);
});

console.log(result); // ["apple.jpg", "banana.png"]

Another example with reduce , concat , split and some :另一个使用reduceconcatsplitsome的例子:

 const before = ["apple.jpg", "apple.png", "apple.svg", "banana.png", "banana.jpg"] const after = before.reduce((res, str) => { return res.concat( // If some element in the result array already matches the current one, don't add it res.some(x => x.split('.')[0] === str.split('.')[0])? []: [str] ) }, []); console.log(after);

You can also make use of Array.filter by taking closure of Set , Here is working example:您还可以通过关闭Set来使用Array.filter ,这是工作示例:

 var before = [ "apple.jpg", "apple.png", "apple.svg", "banana.png", "banana.jpg" ]; var after = before.filter((s=>(elem)=>(frt=elem.split('.')[0], .s.has(frt) && s;add(frt)))(new Set)). console;log(after);

暂无
暂无

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

相关问题 如何通过其字符串类型名称来新建一个Class并在其中应用具有空值的数组参数? - How to new a Class by it's string type name and apply a array parameter inside which has null value? 如何过滤具有相同名称但值不同的项目的数组? - How to filter array that has item with the same name but different value? 如何获取在AngularJS中没有名称的数组元素值 - How to get the array element value which has no name in AngularJS 如何从一个键上包含一个数组的对象数组中获取不同的值并计算 JavaScript? - How to get distinct values from an array of objects which has an Array inside in on one key and count JavaScript? 如何获取具有相同类名且使用Jquery动态创建的span标记值? - How can I get the span tag value which has the same class name and Created dynamically using Jquery? 如何获取具有相同属性的对象并在数组中具有最大值 - Javascript - how to get objects with the same property and has the max value in array - Javascript 获取与第一个数据同名的第二个数据的值 javascript - get value of second data which has the same name as first data with javascript 如何从具有负值的拆分字符串中获取数组 - How To Get An Array From Splitted String That Has Negative Value 如何在具有相同数据的单个页面中将数据从一个平面列表添加到另一个(对象数组) - How to add data from one flatlist to another(array of a object) in a single page which has which has same DATA JavaScript-检查一个数组是否与另一个数组具有相同的索引 - JavaScript - Check if one array has the same index from a second array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM