- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有一个freecodecamp的问题,详情如下:
要求:
创建一个查找对象数组(第一个参数)并返回具有匹配名称和值对的所有对象的数组(第二个参数)的函数。
例如,如果第一个参数是 [{ first: "Romeo", last: "Montague"}, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet"}],并且第二个参数是 { last: "Capulet"},那么您必须返回数组中的第三个对象(第一个参数),因为它包含作为第二个参数传递的名称及其值。
预期结果:
whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" }) should return [{ first: "Tybalt", last: "Capulet" }].
whatIsInAName([{ "apple": 1, "bat": 2 }, { "bat": 2 }, { "apple": 1, "bat": 2, "cookie": 2 }], { "apple": 1, "bat": 2 }) should return [{ "apple": 1, "bat": 2 }, { "apple": 1, "bat": 2, "cookie": 2 }].
whatIsInAName([{ "apple": 1, "bat": 2 }, { "apple": 1 }, { "apple": 1, "bat": 2, "cookie": 2 }], { "apple": 1, "cookie": 2 }) should return [{ "apple": 1, "bat": 2, "cookie": 2 }].
whatIsInAName([{ "apple": 1, "bat": 2 }, { "apple": 1 }, { "apple": 1, "bat": 2, "cookie": 2 }, { "bat":2 }], { "apple": 1, "bat": 2 }) should return [{ "apple": 1, "bat": 2 }, { "apple": 1, "bat": 2, "cookie":2 }].
根据网站,有这样的解决方案:
function whatIsInAName(collection, source) {
var srcKeys = Object.keys(source);
// filter the collection
return collection.filter(function (obj) {
return srcKeys
.map(function(key) {
return obj.hasOwnProperty(key) && obj[key] === source[key];
})
.reduce(function(a, b) {
return a && b;
});
});
}
// test here
whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });
在这个解决方案中,有一件事我不是很理解,那就是map函数的返回值。
之前,在我的预期中,map 函数将循环遍历所有键值对以检查它是否匹配,并返回具有 bool 值的数组,即 [{true, false}, {false, false }] 等,并将 bool 值传递给 reduce 函数。
但是,当我使用以下脚本测试 map 功能时:
var source = { last: "Capulet" };
var collection = [{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }];
var srcKeys = Object.keys({ last: "Capulet" });
collection.filter(function(obj){
return srcKeys.map(function(key){
return obj.hasOwnProperty(key) && obj[key] === source[key];
})
})
返回是这样的
(3) [{…}, {…}, {…}]
0: {first: "Romeo", last: "Montague"}
1: {first: "Mercutio", last: null}
2: {first: "Tybalt", last: "Capulet"}
length: 3
__proto__: Array(0)
在这种情况下,我有两个问题:
在 map 函数中,它创建一个新数组,其中包含对调用数组中的每个元素调用提供的函数的结果。在这种情况下,既然我们只想返回符合条件的元素,为什么不返回 bool 值或者只返回匹配值的元素,而是返回所有值呢?还是我对映射函数的理解有误?
在map函数之后的reduce函数中,如何获取映射的 bool 值到一个单一的 bool 值,表示是否所有srcKey都通过了上面检查的条件?例如,在这种情况下,reduce 函数是否只是简单地获取 map 的返回值并进一步计算?
非常感谢您的提前帮助!
最佳答案
map
部分将 source
的所有键值对映射到一个 bool 值数组,指示该值是否在 obj 中:
var obj = { a: 1, c: 3 };
var source = { a: 1, b: 2, c: 3 };
var mapped = Object.keys(source).map(key => obj[key] === source[key]);
console.log(mapped); // [true, false, true]
现在数组不能作为返回值直接过滤,因为数组永远是真值,不管里面是什么。现在,reducer 将该数组转换为一个 bool 值,如下所示:
[true, false, true].reduce((a, b) => a && b) // false
等同于:
true && false && true // false
所以如果所有键值都存在,最后它返回 true。
PS:停止这门类(class),建议的代码很糟糕。
关于javascript - 在 javascript 中使用 map 和 reduce 来过滤数组中的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52466938/
我的一位教授给了我们一些考试练习题,其中一个问题类似于下面(伪代码): a.setColor(blue); b.setColor(red); a = b; b.setColor(purple); b
我似乎经常使用这个测试 if( object && object !== "null" && object !== "undefined" ){ doSomething(); } 在对象上,我
C# Object/object 是值类型还是引用类型? 我检查过它们可以保留引用,但是这个引用不能用于更改对象。 using System; class MyClass { public s
我在通过 AJAX 发送 json 时遇到问题。 var data = [{"name": "Will", "surname": "Smith", "age": "40"},{"name": "Wil
当我尝试访问我的 View 中的对象 {{result}} 时(我从 Express js 服务器发送该对象),它只显示 [object][object]有谁知道如何获取 JSON 格式的值吗? 这是
我有不同类型的数据(可能是字符串、整数......)。这是一个简单的例子: public static void main(String[] args) { before("one"); }
嗨,我是 json 和 javascript 的新手。 我在这个网站找到了使用json数据作为表格的方法。 我很好奇为什么当我尝试使用 json 数据作为表时,我得到 [Object,Object]
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我听别人说 null == object 比 object == null check 例如: void m1(Object obj ) { if(null == obj) // Is thi
Match 对象 提供了对正则表达式匹配的只读属性的访问。 说明 Match 对象只能通过 RegExp 对象的 Execute 方法来创建,该方法实际上返回了 Match 对象的集合。所有的
Class 对象 使用 Class 语句创建的对象。提供了对类的各种事件的访问。 说明 不允许显式地将一个变量声明为 Class 类型。在 VBScript 的上下文中,“类对象”一词指的是用
Folder 对象 提供对文件夹所有属性的访问。 说明 以下代码举例说明如何获得 Folder 对象并查看它的属性: Function ShowDateCreated(f
File 对象 提供对文件的所有属性的访问。 说明 以下代码举例说明如何获得一个 File 对象并查看它的属性: Function ShowDateCreated(fil
Drive 对象 提供对磁盘驱动器或网络共享的属性的访问。 说明 以下代码举例说明如何使用 Drive 对象访问驱动器的属性: Function ShowFreeSpac
FileSystemObject 对象 提供对计算机文件系统的访问。 说明 以下代码举例说明如何使用 FileSystemObject 对象返回一个 TextStream 对象,此对象可以被读
我是 javascript OOP 的新手,我认为这是一个相对基本的问题,但我无法通过搜索网络找到任何帮助。我是否遗漏了什么,或者我只是以错误的方式解决了这个问题? 这是我的示例代码: functio
我可以很容易地创造出很多不同的对象。例如像这样: var myObject = { myFunction: function () { return ""; } };
function Person(fname, lname) { this.fname = fname, this.lname = lname, this.getName = function()
任何人都可以向我解释为什么下面的代码给出 (object, Object) 吗? (console.log(dope) 给出了它应该的内容,但在 JSON.stringify 和 JSON.parse
我正在尝试完成散点图 exercise来自免费代码营。然而,我现在只自己学习了 d3 几个小时,在遵循 lynda.com 的教程后,我一直在尝试确定如何在工具提示中显示特定数据。 This code
我是一名优秀的程序员,十分优秀!