简体   繁体   English

希望通过使用Javascript map,reduce,foreach,filter方法来实现功能

[英]Want to achieve functionality by using Javascripts map,reduce,foreach,filter methods

Please help me to write better code. 请帮助我编写更好的代码。

Suppose I have a JavaScript array like below: 假设我有一个如下的JavaScript数组:

var students = [
    { firstname: "stud1", lastname: "stud2", marks: "60" },
    { firstname: "stud3", lastname: "stud4", marks: "30" },
    { firstname: "stud5", lastname: "stud6", marks: "70" },
    { firstname: "stud7", lastname: "stud8", marks: "55" },
    { firstname: "stud9", lastname: "stud10", marks: "20" },
];

On page I want to show data in two parts, 1)Students who got marks >= 40 2)Students who got marks < 40 在页面上,我想分两部分显示数据,1)成绩> = 40的学生2)成绩<40的学生

By using for loop I done this functionality easily as below. 通过使用for循环,我可以轻松实现此功能,如下所示。

var data = "";var data1 = "";
for (var i = 0; i < students.length; i++ )
{
    if(students.marks >= 40){
        data += = students.firstname + " " + students.lastname; //actual data in html div tag
        ....
    }else{
            data1 += = students.firstname + " " + students.lastname;
    }
}

I want to achieve same functionality by using JavaScript methods like map, reduce, foreach, filter etc. (want to improve my JavaScript knowledge) 我想通过使用JavaScript方法(例如map,reduce,foreach,filter等)来实现相同的功能(希望提高我的JavaScript知识)

Don't know exactly which method is useful for such functionality. 不确切知道哪种方法对此类功能有用。

Used map method and tried to display data, but there is a trailing , at the end of each object/array. 使用了map方法并尝试显示数据,但在每个对象/数组的末尾都有一个结尾。

Can anyone please help/guide me to write proper code? 任何人都可以帮助/指导我编写正确的代码吗?

Here is solution using filter and forEach methods, using callback functions. 这是使用filterforEach方法以及callback函数的解决方案。

See references here : 在这里查看参考:

 var students = [ { firstname: "stud1", lastname: "stud2", marks: "60" }, { firstname: "stud3", lastname: "stud4", marks: "30" }, { firstname: "stud5", lastname: "stud6", marks: "70" }, { firstname: "stud7", lastname: "stud8", marks: "55" }, { firstname: "stud9", lastname: "stud10", marks: "20" }, ]; students.filter(function(item){ return item.marks>=40; }).forEach(function(item){ div=document.getElementById('persons'); div.innerHTML+=item.firstname+' '+item.lastname+'<br>'; }); 
 <div id="persons"></div> 

You could use a single loop and add the information with predicate as index. 您可以使用单个循环,并以predicate为索引添加信息。 Later join the elements for getting a sting for each element. 稍后加入元素以获取每个元素的刺痛。

 var students = [{ firstname: "stud1", lastname: "stud2", marks: "60" }, { firstname: "stud3", lastname: "stud4", marks: "30" }, { firstname: "stud5", lastname: "stud6", marks: "70" }, { firstname: "stud7", lastname: "stud8", marks: "55" }, { firstname: "stud9", lastname: "stud10", marks: "20" }], predicate = function (o) { return o.marks >= 40; }, result = [[], []]; students.forEach(function (a) { result[+predicate(a)].push(a.firstname + ' ' + a.lastname); }); result = result.map(function (a) { return a.join(', '); }); console.log(result); 

暂无
暂无

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

相关问题 如何在 JavaScript 中使用 filter 来实现 map 功能,因为我们可以使用 reduce 作为 map 和 filter? - How to achieve map functionality with the use of filter in JavaScript, as we can use reduce as map and filter? map()、reduce() 和过滤器与 forEach() - map(), reduce() and filter vs forEach() 是否有另一种方法可以使用map,filter,reduce ...来解决forEach问题? - Is there another way to solve this forEach problem using map, filter, reduce…? 链接数组方法(过滤器、map、reduce)而不是使用双循环 - Chaining array methods (filter, map, reduce) instead of using double loop javascript数组方法(map,forEach,reduce等)的迭代顺序是否确定? - Is the order of iteration for javascript array methods (map, forEach, reduce, etc) deterministic? 是否有任何可能的方法使用.reduce(),。map(),。filter()方法一起排序该数组? - Is there any possible way of sorting that array using .reduce(), .map(), .filter() methods all together? 我想在不使用 forEach 或过滤器或 map 的情况下打印所有元素的索引 - I want to print index of all elements without using forEach or filter or map 在 Ajax 中实现这个精确的 PHP foreach 功能? - Achieve this exact PHP foreach functionality in Ajax? 使用Reduce而不是链接Filter和Map - Using Reduce instead of chaining Filter and Map 在JavaScript中使用带有reduce的map来过滤数组中的对象 - Using map with reduce in javascript to filter objects in an array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM