我正在试验jconsole中的splice()方法
a = [1,2,3,4,5,6,7,8,9,10]
1,2,3,4,5,6,7,8,9,10
Run Code Online (Sandbox Code Playgroud)
这里,a是1到10的简单数组.
b = ['a','b','c']
a,b,c
Run Code Online (Sandbox Code Playgroud)
这是b
a.splice(0, 2, b)
1,2
a
a,b,c,3,4,5,6,7,8,9,10
Run Code Online (Sandbox Code Playgroud)
当我将数组b传递给splice的第三个参数时,我的意思是"从索引零中删除a的前两个参数,并用b数组替换它们".我从来没有见过将数组作为splice()的第三个参数传递(我读过的所有指南页都讨论了一个参数列表),但是,好吧,它似乎可以做到这一点.[1,2]被删除,现在a是[a,b,c,3,4,5,6,7,8,9,10].然后我构建另一个数组,我称之为c:
c = ['one','two','three']
one,two,three
Run Code Online (Sandbox Code Playgroud)
并尝试做同样的事情:
a.splice(0, 2, c)
a,b,c,3
a
one,two,three,4,5,6,7,8,9,10
Run Code Online (Sandbox Code Playgroud)
这次,删除了4个(而不是2个)元素[a,b,c,3],并在开头添加了c数组.有人知道为什么吗?我确信这个解决方案很简单,但我现在还没办法解决.
我一直在尝试实现一个给出两个数组的函数,
array1的元素用作过滤掉array2中元素的条件.
例如:
array1= [apple, grapes, oranges]
array2= [potato, pears, grapes, berries, apples, oranges]
Run Code Online (Sandbox Code Playgroud)
在加入函数之后,array2应该有这样的元素:
filter_twoArrays(array1,array2)
array2= [grapes, apples, oranges]
Run Code Online (Sandbox Code Playgroud)
我尝试了下面的代码,使用for循环和array.splice(),但我看到的问题是,当我使用splice方法时,似乎它改变了for循环中array2的长度:
function filter_twoArrays(filter,result){
for(i=0; i< filter.length; i++){
for(j=0; j< result.length; j++){
if(filter[i] !== result[j]){
result.splice(j,1)
}
}
}
Run Code Online (Sandbox Code Playgroud)
如何优化过滤功能将非常感谢任何输入
干杯!
好的,所以我有一个像这样的数组:
$buttons = array(
'home' => array(
'title' => $txt['home'],
'href' => $scripturl,
'show' => true,
'sub_buttons' => array(
),
'is_last' => $context['right_to_left'],
),
'help' => array(
'title' => $txt['help'],
'href' => $scripturl . '?action=help',
'show' => true,
'sub_buttons' => array(
),
),
'search' => array(
'title' => $txt['search'],
'href' => $scripturl . '?action=search',
'show' => $context['allow_search'],
'sub_buttons' => array(
),
),
'admin' => array(
'title' => $txt['admin'],
'href' => $scripturl . '?action=admin',
'show' => $context['allow_admin'],
'sub_buttons' => array(
'featuresettings' …Run Code Online (Sandbox Code Playgroud) 在用于数组拼接的CoffeeScript文档中,尾随的目的是, _ref什么?
CoffeeScript的:
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
numbers[3..6] = [-3, -4, -5, -6]
Run Code Online (Sandbox Code Playgroud)
编译为:
var numbers, _ref;
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
[].splice.apply(numbers, [3, 4].concat(_ref = [-3, -4, -5, -6])), _ref;
Run Code Online (Sandbox Code Playgroud) i have an array in php full of "Eventos Calendario" objects, at some point in my script i need to introduce a new object of the same type at position x of this array. this is the code i am using
$EventoLimite = new EventosCalendario(null,$Timestamp, $TipoEvento);
var_dump($EventoLimite);
array_splice($this->EventosEntrada, $i, 0, $EventoLimite); //
var_dump($this->EventosEntrada[$i]);
Run Code Online (Sandbox Code Playgroud)
And the "Var_Dumps" i am getting are:
object(EventosCalendario)[15]
public 'FechaHora' => int 1376334000
public 'FechaHoraOriginal' => null
public 'Tipo' => string 'Entrada' (length=7)
public 'Origen' => string …Run Code Online (Sandbox Code Playgroud) 此代码按预期工作,并在值为5或10时删除数组元素.但只有在数组中有1或5或10的值时它才有效.
如果我有超过1的值为5或10,则只删除其中的1个并将其他元素留在数组中.
我的代码:
for($i = 0; $i <= 10; $i++) {
if($somevar[$i] == 5 || $somevar[$i] == 10) {
echo 'the sumvar'.$somevar[$i].' exists<br>';
array_splice($somevar, $i, 1);
}
}
Run Code Online (Sandbox Code Playgroud)
作为一个例子,如果我有:[3, 5, 4]结果如预期:[3, 4].但是,如果我有一个像这样的数组:[3, 5, 10, 4]它只删除5,但不是10 : [3, 10, 4].
我似乎无法找到它我做错了什么以及为什么我的代码不能按预期工作?
我有一个相当标准的项目水合函数,它会抛出“ RangeError:无效的数组长度”。不幸的是,这种情况在生产中很少发生,因此很难捕获输入参数。
这是函数:
function setProgress(items: SomeType[], id: string, someProperty: string) {
const index = items.findIndex(item => item.id === id);
const newItem: SomeType = {...items[index], someProperty };
return [...items.slice(0, index), newItem, ...items.slice(index + 1);
}
Run Code Online (Sandbox Code Playgroud)
这就是转译为的内容(非丑化):
function setProgress(items, id, someProperty) {
var index = items.findIndex(function(e) {
return items.id === id
}),
newItem = Object.assign({}, items[index], {
someProperty: someProperty
});
return items.slice(0, index).concat([newItem], items.slice(index + 1));
}
Run Code Online (Sandbox Code Playgroud)
我尝试过使用不同的值,但无法重现此错误。
知道是什么以及如何导致这种情况吗?
给出以下数组:
Array
(
[143] => Car #1
[144] => Car #2
[145] => Car #3
)
Run Code Online (Sandbox Code Playgroud)
我目前正在使用它
implode(', ', array_values($car_names))
Run Code Online (Sandbox Code Playgroud)
生成一个类似的字符串
汽车#1,汽车#2,汽车#3
我想实际上得到类似的东西
汽车#1,汽车#2和汽车#3
想法是在数组的最后两个元素之间插入"和".
如果数组恰好包含两个键/值对(例如,用户有2辆车),则不会有逗号.
汽车#1和汽车#2
如果数组包含一个键/值(例如,用户有1辆车)
汽车#1
有任何建议如何完成这项工作?我尝试过使用,array_splice但我不确定是否可行(即将新元素插入数组).
谢谢你的帮助!
假设我有一个这样的数组:
myArray = ["a","b","c","d","e"]
Run Code Online (Sandbox Code Playgroud)
我想遍历它以查找特定值并删除它们。
for(var i=0;i<myArray.length;i++){
if(myArray[i] == "b")
myArray.splice(i,1)
}
Run Code Online (Sandbox Code Playgroud)
问题是, splice 从数组中删除了该项目,并且删除的项目前面的所有项目都向下移动一个索引号,因此myArray.length被实例化为 5 但在splice myArray长度仅为 4 之后for循环失败,因为myArray[4]throws typeof框架中的匹配错误。
我正在使用以这种方式工作的框架,这就是为什么我要使用这样的项目删除技术,我的问题是如何以正确的方式执行此操作?该框架使用该splice方法,我使用的是for循环,所以我认为有正确的方法来解决这个问题?
我有一个列表,并且正在使用for循环遍历它。结构如下:
salesLists: {
1: [ [], [], [] ]
2: [ [], [] ]
}
Run Code Online (Sandbox Code Playgroud)
和html:
<div v-for="(saleLists, index) in salesLists">
<my-comp v-for="(item, i) in saleLists" :key="i" :index="parseInt(i)+1"></my-comp>
</div>
Run Code Online (Sandbox Code Playgroud)
现在,我正在尝试从salesLists[1]数组中删除项目。我有一个按钮,用于@click="removeForm":
removeForm(e) {
var index = parseInt(e.target.getAttribute('data-index')) - 1 // = 2
var client = e.target.getAttribute('data-client') // = 1
//Vue.delete(this.salesLists[client], index);
this.salesLists[client].splice(index, 1)
this.$forceUpdate()
}
Run Code Online (Sandbox Code Playgroud)
但是,它删除了它,因为我没有指定任何键,并且它只是空数组(我认为),所以没有从DOM中删除正确的元素。它删除了索引2,但是当它v-for遍历该项目并减少计数时,它仅删除了最后一个项目。
解决此问题的正确方法是什么?:/
这是一个小提琴:https : //jsfiddle.net/8rvfz40n/ 尝试为每个输入字段编写不同的值并删除中间的一个,您将看到它将删除最后一个
array-splice ×10
arrays ×8
javascript ×6
php ×4
apply ×1
coffeescript ×1
filter ×1
for-loop ×1
implode ×1
loops ×1
object ×1
slice ×1
splice ×1
typescript ×1
vuejs2 ×1