JavaScript 函数比较数组中的值和 return 它
JavaScript function to compare values in the array and return it
模组:请随意解释这个问题。
我想做的是判断数组中给定 dynamic 属性 的所有值是否相同,如果 true ,return吧。
我使用 underscore.js 在 3 行中实现了这一点,但是,我希望这可能是 simplified/shortened:
var val = this.children[0]["myProperty"];
var same = _.all(this.children, child => child["myProperty"] === val);
return same ? val : null;
所以如果:
this.children = [{"myProperty":null},{"myProperty":2},{"myProperty":1},{"myProperty":1}];
...returns null
this.children = [{"myProperty":1},{"myProperty":1},{"myProperty":1},{"myProperty":1}];
...returns 1
N.B。 "myProperty" 的值是 个整数或 null
这似乎有效,使用香草 javascript 和 reduce
,或者它可能会把你推向正确的方向:
function children(arr,prop){
return arr.reduce(
function(prevVal,curVal){
return prevVal == curVal[prop] ? prevVal : null
},
arr[0][prop]
);
}
children([{"myProperty":null},{"myProperty":2},{"myProperty":1},{"myProperty":1}],'myProperty')
> null
children([{"myProperty":1},{"myProperty":1},{"myProperty":1},{"myProperty":1}],'myProperty')
> 1
或
Array.prototype.children = function(prop){
return this.reduce(
function(prevVal,curVal){
return prevVal == curVal[prop] ? prevVal : null
},
this[0][prop]
);
}
[{"myProperty":null},{"myProperty":2},{"myProperty":1},{"myProperty":1}].children('myProperty')
> null
[{"myProperty":1},{"myProperty":1},{"myProperty":1},{"myProperty":1}].children('myProperty')
> 1
动态 属性:
var prop = "myProperty";
//vanilla JS
return this.children.map(o => o[prop]).reduce((acc, v) => acc === v? v: null);
//and underscore
return _.reduce(_.pluck(this.children, prop), (acc, v) => acc === v? v: null);
您自己的解决方案 returns 尽快否定结果。它仍然是 O(n),但是如果 children
很大并且值很少相等,那么差异可能是明智的(几乎从不)。在普通 JS 中,使用 .every
:
function childrenValue(prop) {
var first = this.children[0][prop];
return this.children.every(child => child[prop] === first) ? first : null;
}
模组:请随意解释这个问题。 我想做的是判断数组中给定 dynamic 属性 的所有值是否相同,如果 true ,return吧。
我使用 underscore.js 在 3 行中实现了这一点,但是,我希望这可能是 simplified/shortened:
var val = this.children[0]["myProperty"];
var same = _.all(this.children, child => child["myProperty"] === val);
return same ? val : null;
所以如果:
this.children = [{"myProperty":null},{"myProperty":2},{"myProperty":1},{"myProperty":1}];
...returns null
this.children = [{"myProperty":1},{"myProperty":1},{"myProperty":1},{"myProperty":1}];
...returns 1
N.B。 "myProperty" 的值是 个整数或 null
这似乎有效,使用香草 javascript 和 reduce
,或者它可能会把你推向正确的方向:
function children(arr,prop){
return arr.reduce(
function(prevVal,curVal){
return prevVal == curVal[prop] ? prevVal : null
},
arr[0][prop]
);
}
children([{"myProperty":null},{"myProperty":2},{"myProperty":1},{"myProperty":1}],'myProperty')
> null
children([{"myProperty":1},{"myProperty":1},{"myProperty":1},{"myProperty":1}],'myProperty')
> 1
或
Array.prototype.children = function(prop){
return this.reduce(
function(prevVal,curVal){
return prevVal == curVal[prop] ? prevVal : null
},
this[0][prop]
);
}
[{"myProperty":null},{"myProperty":2},{"myProperty":1},{"myProperty":1}].children('myProperty')
> null
[{"myProperty":1},{"myProperty":1},{"myProperty":1},{"myProperty":1}].children('myProperty')
> 1
动态 属性:
var prop = "myProperty";
//vanilla JS
return this.children.map(o => o[prop]).reduce((acc, v) => acc === v? v: null);
//and underscore
return _.reduce(_.pluck(this.children, prop), (acc, v) => acc === v? v: null);
您自己的解决方案 returns 尽快否定结果。它仍然是 O(n),但是如果 children
很大并且值很少相等,那么差异可能是明智的(几乎从不)。在普通 JS 中,使用 .every
:
function childrenValue(prop) {
var first = this.children[0][prop];
return this.children.every(child => child[prop] === first) ? first : null;
}