我需要在typescript对象数组中添加值
如果数组是
的示例[
{'title':'this is post title1','content':'this is post content1'},
{'title':'this is post title2','content':'this is post content2'},
{'title':'this is post title3','content':'this is post content3'},
{'title':'this is post title4','content':'this is post content4'},
{'title':'this is post title5','content':'this is post content5'},
]
我希望当我把新项目放在这个数组的第一个中时,比如jQuery中的prepend
[
{'title':'this is new item title','content':'this is new item content'},
{'title':'this is post title1','content':'this is post content1'},
{'title':'this is post title2','content':'this is post content2'},
{'title':'this is post title3','content':'this is post content3'},
{'title':'this is post title4','content':'this is post content4'},
{'title':'this is post title5','content':'this is post content5'},
]
提前致谢
答案 0 :(得分:2)
使用jquery无法完成,因为它的功能属于javascript数组对象。
您可以使用数组函数unshift()
。
答案 1 :(得分:1)
您可以使用...
运算符
例如:
let myArray = [ 1,2,3,4,5 ];
myArray = [0, ...myArray];
答案 2 :(得分:1)
您可以使用unshift
将项目添加到数组中:
const myArray = [ 2, 3, 4 ]
myArray.unshift(1)
console.log(myArray); // [ 1, 2, 3, 4 ]
您可以在此处找到文档:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/unshift
答案 3 :(得分:1)