在以下reduction + map操作中,没有。3让我感到困惑。谁能解释为什么
// 1
[1,2,3,4,5].filter(x => x==3).reduce((x, y) => y) // -> 3, all good
// 2
[1,2,3,4,5].filter(x => x<=3).reduce((x, y) => 0) // -> 0, still good
// 3
[1,2,3,4,5].filter(x => x==3).reduce((x, y) => 0) // -> 3, hello?
Run Code Online (Sandbox Code Playgroud)
换句话说:如何减少一个元素的数组而忽略0操作映射?最终,它将用于对象数组,因为对象数组.reduce((x,y) => y.attr)也返回y而不是y.attr用于单个元素数组。
这是小提琴
$(".listitems li").sort(sort_li).appendTo('.listitems');
function sort_li(a, b){
return ($(b).data('position')) < ($(a).data('position')) ? 1 : ($(b).data('position2')) > ($(a).data('position2')) ? 1 : 0;
}
Run Code Online (Sandbox Code Playgroud)
如何按第一个属性position然后按第二个属性对其进行排序position2
我想要得到的结果:
Item 24 40549
Item 24 40501
Item 24 40500
Item 3 14801
Item 3 14800
Item 1 90500
Item 0 0
Item 0 0
Item 0 0
Run Code Online (Sandbox Code Playgroud) 我有两个相同的 <ul>列表,当我按下按钮时,我需要使用jQuery来反转两个列表的顺序.我已经尝试了波纹管代码并且它没有按预期工作.
function spin(){
spinList($('.list-view'));
}
var spinList = function($el){
var elementClone = $el.eq(0).clone();
var allLength = elementClone.find('li').length;
for(var i = 0;i<allLength;i++){
var element = elementClone.find('li').eq(allLength-i).clone();
$el.eq(0).find('li').eq(i).replaceWith(element);
$el.eq(1).find('li').eq(i).replaceWith(element);
}
} Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button onclick="spin()">Change order</button>
<ul class="list-view">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
<li>8</li>
<li>9</li>
<li>10</li>
<li>11</li>
<li>12</li>
</ul>
<ul class="list-view">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
<li>8</li>
<li>9</li>
<li>10</li>
<li>11</li>
<li>12</li>
</ul>Run Code Online (Sandbox Code Playgroud)
我想根据键来连接数组中的项目.我跑通了lodash库尝试_.join,_.merge(),_.groupBy()但没有那些似乎工作.你如何根据他们的密钥加入数组中的项目?
var myArray = [
{name: 'cheesesteak'},
{name: 'hamburger'},
{name: 'pork roll'}
];
var jawn = myArray.join();
console.log(jawn);
Run Code Online (Sandbox Code Playgroud)
理想的输出是......
"chessesteak, hamburger, pork roll"
Run Code Online (Sandbox Code Playgroud) 我有一个产品订购单,有不同尺寸可供选择。因此,对于每个产品,每个尺寸都有一个输入,总和位于行的末尾。我现实我有大约 500 行。
<table>
<tr>
<th>Product</th>
<th>Size 1</th>
<th>Size 2</th>
<th>Size 3</th>
<th>TOTAL</th>
</tr>
<tr>
<td>A</td>
<td><input type="text" class="productA"></td>
<td><input type="text" class="productA"></td>
<td><input type="text" class="productA"></td>
<td></td>
</tr>
<tr>
<td>B</td>
<td><input type="text" class="productB"></td>
<td><input type="text" class="productB"></td>
<td><input type="text" class="productB"></td>
<td></td>
</tr>
<tr>
<td>C</td>
<td><input type="text" class="productC"></td>
<td><input type="text" class="productC"></td>
<td><input type="text" class="productC"></td>
<td></td>
</tr>
</table>Run Code Online (Sandbox Code Playgroud)
我尝试了几种方法,但它从来不想工作。我只成功地一次计算了所有输入,而不仅仅是单独计算了一行。
td每次输入更改时,我想计算最后每一行的总和。
有谁知道我如何解决这个问题?
我期待改变一些数据的结构.以下是我收到的数据样本
{
ID1: {
firstName: 'John',
lastName: 'Doe'
},
ID2: {
firstName: 'Jane',
lastName: 'Doe'
}
}
Run Code Online (Sandbox Code Playgroud)
我需要做的是将其转换为具有以下格式的数组:
[
{ ID: ID1, firstName: 'John', lastName: 'Doe' },
{ ID: ID2, firstName: 'Jane', lastName: 'Doe' }
]
Run Code Online (Sandbox Code Playgroud)
如果可能的话,我更喜欢使用lodash
嗨,我正在尝试使用for循环在javascript中推送数组内的javascript对象来迭代数据.这是我的代码的样子.
var data = {"up": [{
"name": "jack",
"age" : 10
},
{
"name" : "jhon",
"age" : 12
}]};
var output = {};
var output_data = {
element: []
};
for (var key in data.up) {
output.user_name = data.up[key].name;
output_data.element.push(output);
}
console.log(output_data.element);
Run Code Online (Sandbox Code Playgroud)
但是,正如您在示例http://jsbin.com/fanazaxoda/edit?html,js,console中看到的那样,只有第二个元素名称'jhon'被插入两个世界中.我在这做错了什么?请解释.
这应该引用对象本身但在下面的代码中为什么它的行为不同?
var x = 4,
obj = {
x: 3,
bar: function() {
var x = 2;
setTimeout(function() {
var x = 1;
alert(this.x);
}, 1000);
}
};
obj.bar();
Run Code Online (Sandbox Code Playgroud)
警报为什么4而不是3?
我有一个包含算术运算符的字符串数组,我想用新的算术运算符替换数组中的那些算术运算符。
例如:
var equation = '5.0 + 9.34 - 6.0 * 2.1 * 3.1 / 2.0';
var newEquation = equation.replace(/+-*//, '+');
Run Code Online (Sandbox Code Playgroud)
但是,它不会更改为想要的结果。请指教。非常感谢您的贡献。
var alph = ["a", "b", "c"];
var r = [];
for(var i = 0; i < 5; i += 1) {
r.push(alph);
}
r[0].reverse();
console.log(r);
/* Output
[ [ 'c', 'b', 'a' ],
[ 'c', 'b', 'a' ],
[ 'c', 'b', 'a' ],
[ 'c', 'b', 'a' ],
[ 'c', 'b', 'a' ] ]
*/
/* Expected output
[ [ 'c', 'b', 'a' ],
[ 'a', 'b', 'c' ],
[ 'a', 'b', 'c' ],
[ 'a', 'b', 'c' ],
[ …Run Code Online (Sandbox Code Playgroud)