每一个在我的列表中的元素的一个数组持有的意见,如Javascript - 如何将特定索引中的元素插入到数组的末尾?
for (let i = 0; i < myList; i ++) {
myList[i][‘comments’] = [];
}
我的失败尝试:
if (someCondition) {
// insert from index k to the end of the array
myList[‘comments’].splice(k, 0, “newElement”);
}
一个例子:
myList = [ “comments”: [“1, 2”], “comments”:[], “comment”: [“2”, “2”], “comment”: [] ]
目标: 插入来自索引2
myList = [ “comments”: [“1, 2”], “comments”:[], “comment”: [“2”, “2”, “newElement"], “comment”: [“newElement”] ]
只是为了澄清,“插入”是指您移动元素还是复制元素?所以如果你有一个数组'a = [“a”,“b”,“c”,“d”]'那么将会从索引1插入'[“a”,“c”,“d “,”b“]或'[”a“,”b“,”c“,”d“,”b“]'? – JJJ
我想OP希望'myList ['comments']。push(myList ['comments']。splice(k,1)[0])' – mhodges
@JJJ新增上面的例子 –