最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to prepend new item to array in typescript - Stack Overflow

programmeradmin1浏览0评论

i need to prepend value in typescript objects array

Example if array is

[
   {'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'},
]

i want when i put new item in the first of this array like prepend in jQuery

[
   {'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'},
]

thanks in advance

i need to prepend value in typescript objects array

Example if array is

[
   {'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'},
]

i want when i put new item in the first of this array like prepend in jQuery

[
   {'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'},
]

thanks in advance

Share Improve this question edited Dec 11, 2024 at 20:57 CharybdeBE 1,8431 gold badge21 silver badges37 bronze badges asked Mar 9, 2018 at 13:35 RoufailRoufail 5731 gold badge8 silver badges24 bronze badges 4
  • Same way as in JS. typescript tag is for TS-specific questions. – Estus Flask Commented Mar 9, 2018 at 13:40
  • .unshift() TypeScript is a syntactic sugar of JavaScript. So You should google JavaScript a developer.mozilla.org/pl/docs/Web/JavaScript/Referencje/Obiekty/… – Anton Temchenko Commented Mar 9, 2018 at 13:43
  • Possible duplicate of How can I add new array elements at the beginning of an array in JavaScript? – Mohammad Usman Commented Mar 9, 2018 at 13:50
  • The example you gave is javascript. You should create a class in typescript for it. – Jeroen van Langen Commented Mar 9, 2018 at 14:03
Add a comment  | 

4 Answers 4

Reset to default 9

You can use unshift to prepend items to an array:

const myArray = [ 2, 3, 4 ]

myArray.unshift(1)

console.log(myArray); // [ 1, 2, 3, 4 ]

You can find the docs here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/unshift

You can use the ... operator

Eg :

 let myArray = [ 1,2,3,4,5 ];
 myArray = [0, ...myArray];

It cannot be done with jquery, because it is functionality belongs to the javascript array object.

You could use the array function unshift() for that.

You can solve it with splice.

arr.splice(index, 0, item);

From another SO question

发布评论

评论列表(0)

  1. 暂无评论